问题描述:

I added the following code in my iOS app to delete table cell from UITableViewController, but I get an error about the deletion.

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if(editingStyle == UITableViewCellEditingStyleDelete){

[self.toDoItems removeObjectAtIndex:indexPath.row];

NSArray *indexPaths = @[indexPath];

[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

Update:

toDoItems is just an NSArray, and I have tried the following code but is still not working:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if(editingStyle == UITableViewCellEditingStyleDelete){

NSArray *indexPaths = @[indexPath];

[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

[self.toDoItems removeObjectAtIndex:indexPath.row];

}

}

the error message is here:

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (1003) must be equal to the number of rows contained in that section before the update (1004), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Xcode 6.2 iOS6

I have already tried Deleting table cell causes crash

网友答案:

The problem is that you are deleting the wrong object from your data model:

[self.toDoItems removeObjectAtIndex:indexPath.row];

That's too simple-minded. You have multiple sections in this table. You need a data model that distinguishes these sections, and you need to delete the correct row from the correct section in your data model before deleting the corresponding row from the table. You cannot do that by looking at indexPath.row alone; you need to structure your data model so as to take account of indexPath.section as well.

相关阅读:
Top