我有一个
UITableViewController
,显示一堆“n”行(全部使用标准 UITableViewCell
类),最后一行有一个图像(一个小“+”图标表示“添加记录”)。
假设以下场景:
UITableViewController
,我调用 [self.tableView reloadData]
以确保显示新状态您现在看到“n+1”行,最后一行现在显示“+”图像。问题是我遇到了一个错误,其中行“n”仍然显示“+”图像,只是它与该单元格的文本标签重叠(我可以理解该错误,它向文本左侧 - 我明确设置了
imageView.image = nil
来避免这种情况 - 但图像与文本重叠这一事实使得这个非常奇怪)。
这是我的
cellForRowAtIndexPath
函数的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
UITableViewCellStyle cellStyle;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if ((self.selectedCategory != nil) && (indexPath.row < [self.selectedCategory.subjects count])) {
Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row];
cell.textLabel.text = managedObject.name;
cell.imageView.image = nil; // Needed in case there used to be a "+" icon
} else {
cell.textLabel.text = @"Add subject...";
[cell.imageView initWithImage:_addIcon];
}
return cell;
}
我在我的一个应用程序中有一个类似的功能,其中我有一个加载更多按钮作为最后一个单元格。我对最后一个单元格使用了不同的标识符,它对我有用。
if(indexPath.row==[self.selectedCategory.subjects count])
{
cellIdentifier = @"PlusCell";
}
这对我有用。
替换:
static NSString *CellIdentifier = @"Cell";
在
- (