Reputation: 6966
I have a UITableViewController
that displays a bunch of 'n' rows (all using the standard UITableViewCell
class), the last of which has an image (a little '+' icon to indicate 'Add record').
Suppose the following scenario:
UITableViewController
, and I call [self.tableView reloadData]
to make sure the new state is displayedYou now see 'n+1' rows, the last of which now shows the '+' image. The problem is that I'm experiencing a bug where row 'n' still shows the '+' image, except that it overlaps with the text label for that cell (I could understand the bug where it showed the '+' image to the left of the text - I explicity set imageView.image = nil
to avoid this - but the fact that the image overlaps with the text makes this one really weird).
Here's the code for my cellForRowAtIndexPath
function:
- (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;
}
Upvotes: 0
Views: 939
Reputation: 466
This worked for me.
replace :
static NSString *CellIdentifier = @"Cell";
in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
with :
NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
Upvotes: 1
Reputation: 26390
I had a similar feature in one of my apps where i had a load more button as the last cell. I used different identifiers for last cell and it worked for me.
if(indexPath.row==[self.selectedCategory.subjects count])
{
cellIdentifier = @"PlusCell";
}
Upvotes: 1