Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView deleteRowsAtIndexPaths not working

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?

like image 561
Nick ONeill Avatar asked Nov 10 '22 15:11

Nick ONeill


1 Answers

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.

like image 108
Senseful Avatar answered Nov 15 '22 06:11

Senseful