My table view is populated from an array containing data in the core data, I want to delete rows while updating the core data correspondingly, here is my code of deletion.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
NSManagedObject *selectedObject = [arrayList objectAtIndex:[indexPath row]];
[managedObjectContext deleteObject:selectedObject];
[arrayList removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
However, I am getting the following error when I hit delete button:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995
*** 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).'
My data source is array开发者_运维技巧List
containing NSManagedObjects fetched from core data.
Ideas? Thanks in advance!
Update:
Now the deletion can work after I remove the data in the arrayList, but data in core data didn't get deleted correspondingly, when app relaunches, the list is still the same.
It means that even after you deleted the row, the datasource is still returning 4 as the number of rows in section 0. So now the table view only has 3 rows displayed for a section that should have 4.
From your description, you probably need to delete the object from arrayList
in addition to deleting it from CoreData
. Alternatively reload arrayList
after deleting the object from CoreData
and then delete the row.
精彩评论