andygeers
andygeers

Reputation: 6966

UITableViewCell still shows image overlapping the text after clearing

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:

  1. The user clicks on that last row, which pushes a new view controller onto the navigation stack
  2. The user then fills out a form to add a new record, which is saved using Core Data
  3. The form is then popped from the navigation stack, returning you to that first UITableViewController, and I call [self.tableView reloadData] to make sure the new state is displayed

You 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

Answers (2)

iCodeAtApple
iCodeAtApple

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

visakh7
visakh7

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

Related Questions