if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
Budget *deletedBudget = (Budget *)[self.budgetList objectAtIndexPath:indexPath];
[self.context deleteObject:deletedBudget];
NSError* error;
if(![self.context save:&error]) {
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else {
NSLog(@" something wierd happens ... hmmm");
}
}else {
NSLog(@"budget deleted!!!! YEEEEE");
}
[self updateBugetList];
[self.tableView beginUpdates];
if ([self.budgetList.fetchedObjects count] >= 1) {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if ([self.budgetList.fetchedObjects count] == 0) {
//[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView reloadData];
}
[self.tableView endUpdates];
}
此代码适用于大多数行,但不适用于最后一行。
当最后一行的删除被点击时,显示实体被删除的日志消息,但该行没有从表视图中删除......
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).'
如何避免这种情况?