So I have this code to delete a row in my tableview:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.cellArray removeObjectAtIndex:indexPath.row/2];
[[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
}
Now I do indexPath.row/2 because I do this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cellArray count] * 2;
}
Just because its what makes my UI look the way it is. Anyway this is the crash:
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (42) must be equal to the number of rows contained in that section before the update (44), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
[self.cellArray removeObjectAtIndex:indexPath.row/2];
[[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
The problem is that you are using two table rows per object in your cell array. But in the code you posted, you only delete one row from the table. You need to delete two rows.
BTW –
YESis not a valid argument for the row animation.