I am trying to delete a cell on tableView with sliding my finger (like on the recent call). I know I need to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *index = [[NSArray alloc]initWithObjects:indexPath];
[self.tableView deleteRowsAtIndexPaths:index withRowAnimation: UITableViewRowAnimationNone];
}
But when I slide the finger, I get to this function and in the second row when I am trying to delete the cell an get error:
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 (4) must be equal to the number of
rows contained in that section before the update (4), plus or minus
the number of rows inserted or deleted from that section (0 inserted,
1 deleted).
Can someone help?
When you call
The table view will try and reload, which in turn calls the delegate method
Since you have deleted one row, for the necessary animations to work correctly, the table view requires the number of rows to change by the same amount that you deleted with deleteRowsAtIndexPaths:withRowAnimation:. The error message you gets tries to explain that dependency. It is saying you had 4 rows in the table view, you deleted 1… but when the table view asked you how many rows you had after the deletion, you said 4 again instead of 4-1 = 3 which it expected… It then proceeded to off itself presumably because it did not want to live in a world where the laws of natural numbers could not explain reality.
For example if you are using an NSArray* to populate your tableView, you should be deleting the corresponding objects from the array at the same time as you are calling deleteRowsAtIndexPaths:withRowAnimation:.