uitableview delete rows


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

如何避免这种情况?

我对这种情况有点困惑:

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

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

最新更新