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).'
如何避免这种情况?
答案 0 :(得分:0)
我对这种情况感到困惑:
if ([self.budgetList.fetchedObjects count] >= 1) {
...
if ([self.budgetList.fetchedObjects count] == 0) {
...
[tableView reloadData];
}
[tableView endUpdates];
}
似乎你永远不会更新表,因为第二个条件不会运行。 我认为你应该在这种情况下重新加载表