我有一个表格视图,里面有一些按钮。一行中有4个按钮,第二行中有另外4个按钮,因此一个按钮。我正在使用数组将图像添加到按钮中。
第一次它正常工作但是当删除数组中的所有项目并将新对象删除到数组然后调用reload table视图时,不会重新加载新数据。
可能是什么原因?
我发布了部分代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// }
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button1 setImage:[array objectAtindex:0]
[button1 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:self.button1];
button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button2 setImage:[array objectAtindex:1] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
}
else if(indexPath.row == 1)
{
button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)];
[button3 setImage:[array objectAtindex:2] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)];
[button4 setImage:[array objectAtindex:3] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button4];
}
else
{
// Do something here
}
return cell;
}
我的点击按钮:
-(IBAction)clickToDisplayImage {
[text resignFirstResponder];
if([imageArray count] >0)
{
[imageArray removeAllObjects];
}
int totalImages = [text.text intValue];
for( int i=0;i<totalImages;i++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]];
}
[tableView reloadData];
}
答案 0 :(得分:0)
当您在 UITableView 上调用reloadData时,将调用 cellForRowAtIndexPath 消息来填充该行。因为您已经从第一次传递中创建了单元格,所以当您调用dequeueReusableCellWithIdentifier时,您将获得与@“Cell”标识符关联的最初创建的单元格。
您可以通过设置断点并检查第二次通过 dequeueReusableCellWithIdentifier (而不是新创建的单元格)获取您的单元格来确认这一点。然后,您将向该原始单元格添加新的子视图(因此单元格的四个按钮子视图,然后是六个,八个等...)。
你应该做的只是在你第一次创建单元格时添加按钮子视图(所以将按钮创建东西移动到if(cell == nil)块中)你应该使用基于表格的单元格标识符行。类似的东西:
- (UIButton*) getButtonLeft:(BOOL)left atIndex:(int)index
{
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake((left ? 15 : 135),5,100,87)];
[button setImage:[array objectAtindex:index] forState:UIControlStateNormal];
[button addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// both rows are different so we should use a different re-use id.
NSString * CellIdentifier= [NSString stringWithFormat:@"Cell%d", indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
switch (indexPath.row)
{
case 0:
button1 = [self getButtonLeft:YES atIndex:indexPath.row];
[cell.contentView addSubview:button1];
break;
case 1:
button2 = [self getButtonLeft:NO atIndex:indexPath.row];
[cell.contentView addSubview:button2];
break;
case 2:
button3 = [self getButtonLeft:YES atIndex:indexPath.row];
[cell.contentView addSubview:button3];
break;
case 3:
button4 = [self getButtonLeft:NO atIndex:indexPath.row];
[cell.contentView addSubview:button4];
break;
default:
// other cells?
break;
}
}
// if you need to modify the cell you have dequeued/created this is where you would do that
return cell;
}
<强> N.B。我没有通过编译器运行此代码,因此出现语法错误;)