Reputation: 4042
My tableview datasoure comes from a array, and it gets data from managed object context's executeFetchRequest method. In the commitEditingStyle delegate, I got this 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 (1) must be equal to the number of rows contained in that section before the update (1),
the delegate:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedOjbectContext deleteObject:[self.myEvents objectAtIndex:indexPath.row]];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
Upvotes: 1
Views: 784
Reputation: 514
If you delete a cell from you TableView, your datasource needs to be updated to recognize these changes.
There is a method called numberOfRowsInSection: This method returns the number of cells the table view should be displaying. More than likely its using an array or some other type of data structure. You need to make sure that this array is kept in sync with any changes made to the table view itself. If you tell the table view to remove a view, you need to also update the backing data of the table view to reflect the changes.
Upvotes: 0
Reputation: 438
Why not encapsulate the deleting and all between [self.tableView beginUpdates]
and [self.tableView endUpdates]
?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
[self.managedOjbectContext deleteObject:[self.myEvents objectAtIndex:indexPath.row]];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
As per: Apple's Table View Programmming Guidelines
Upvotes: 0
Reputation: 17655
try to use [tableView beginUpdates];
and [tableView endUpdates];
dont use reloadData
like this
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedOjbectContext deleteObject:[self.myEvents objectAtIndex:indexPath.row]];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
Upvotes: 0
Reputation: 14662
The error says it all, you removed an a row, but your delegate stills says it's there.
My guess is that you didn't remove your object from self.myEvents
. Removing the object from the managed context doesn't remove it from your array.
Assuming self.myEvents
is an NSMutableArray*
[self.myEvents removeObjectAtIndex:indexPath.row]
Upvotes: 1
Reputation: 2980
You should delete the entry from your NSArray and then reload the tableView, that way won't have inconsistencies...
Upvotes: 1