5

I'm not quite sure what's going on here but I'm running into a contact form that I'm working with. In my form I've added the ability to add an email address to a contact. As pictured below, once the user clicks on "add email", a row is added to the "Emails" section.

Create contact image

However after I click to delete the email, an extra cell appears underneath the add email button (pictured below).

before and after

There's a little red box from what appears to be the prior delete cell as though the table isn't reloaded. Here's my delete code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if( editingStyle == UITableViewCellEditingStyleDelete )
    {
        // Update the data source
        NSMutableArray *fields = (NSMutableArray *)self.fields[@(indexPath.section)];
        [fields removeObjectAtIndex:indexPath.row];
        NSMutableArray *values = (NSMutableArray *)self.values[@([self fieldTypeAtIndexPath:indexPath])];
        [values removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}

So why is that cell remaining afterwards? I can call [tableView reloadData] within the method above and it removes the excess cell but then it messes up the animation. Can you shed some clarity on what I'm doing wrong here?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Nick ONeill
  • 7,341
  • 10
  • 47
  • 61

3 Answers3

1

This looks like a bug.

I just noticed that even Apple has this same bug in their Reminders app. It seems to be related to deleting rows from a UITableView with variable height items.

In Apple's Reminders app, when you delete something from a long list with variable height items, you will see the same exact visual artifact for a split second. Then the table jumps and the list looks correct. I am assuming Apple just reloads the entire table view a second after the item is removed in order to fix the visual glitch.

My recommendation is to report this as a bug to Apple. For now you can reload the entire table view like Apple presumably does.

Senseful
  • 86,719
  • 67
  • 308
  • 465
  • I'm having the exact same issue. Even when I reload, the deleted cell gets dequeued and shows up in its delete state (Delete is visible). – Armin Apr 17 '14 at 17:34
0

Option 1: Before you call deleteRowsAtIndexPaths, you must make sure that numberOfRowsInSection will return the correct value (e.g. if you originally had 2 rows in that section, it should now return 1 row).

Option 2: If that isn't the problem, I would try a different animation type (e.g. UITableViewRowAnimationAutomatic), just to see if that has any effect.

Option 3: Ensure that cellForRowAtIndexPath is returning a valid cell with its contents being reset. Otherwise it may be reusing an invalid cell or displaying something that was there before.

Option 4: Since the heights are different, ensure that heightForRowAtIndexPath is returning the correct value. You probably need to call reloadRowsAtIndexPaths in order for the table view to know about the height difference.

Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465
0

I had the same problem and I guess it might be a bug in iOS7 where it doesn't repaint the cells correctly, I fixed it but not in an efficient way, just in the "cellForRowAtIndexPath" , always create a new cell, don't dequeue it.