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).'
どうすればこれを回避できますか?