0
  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).'

如何避免这种情况?

4

1 回答 1

0

我对这种情况有点困惑:

    if ([self.budgetList.fetchedObjects count] >= 1) {
...

        if ([self.budgetList.fetchedObjects count] == 0) {
...            
         [tableView reloadData];
        }
        [tableView endUpdates];
    }

似乎您永远不会更新表格,因为第二个条件不会运行。我认为在这种情况下你应该总是重新加载表

于 2012-10-06T22:11:32.593 回答