UITableView deleteRowsAtIndexPaths not working

Viewed 2527

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?

3 Answers
Related