我知道这已经被问过 50 次,但我查看了所有其他答案,但没有找到任何可以解决我的问题的东西。
无论如何,这是我的代码:
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
[self.cellArray removeObjectAtIndex:i];
}
}
[thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
当我执行这段代码时,我得到了这个崩溃日志:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
有谁看到我做错了什么?
新代码:
NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [thetableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
if ([[thetableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
[mutableIndexSet addIndex:p.row];
}
}
[self.cellArray removeObjectsAtIndexes:mutableIndexSet];
[thetableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
[mutableIndexSet release];