我正在尝试使用《 Apple Table View编程指南》中所述的标准编辑控件样式来实现UITableViewCell的标准删除。在commitEditingStyle方法中执行以下代码时
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
我收到以下错误消息
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第2节中的行数无效。更新(1)之后现有节中包含的行数必须等于该行中包含的行数更新(1)之前的部分,加上或减去从该部分插入或删除的行数(已插入0,已删除1)。
最佳答案
如果这是唯一的行,建议您也删除该部分。您还需要将数据源与表同步,即,也删除数据源中的行。
当文件系统是您在iOS中的数据源时,这是一个正确的示例:
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
File *file = (File *)[self.files objectAtIndex:indexPath.row];
[[NSFileManager defaultManager] removeItemAtPath:file.path error:nil];
[self.files removeObject:file];
if (indexPath.row == 0 && [self.files count] == 0) {
NSInteger sectionIndex = [indexPath indexAtPosition:0];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
}else {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self setupData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
关于iphone - 删除UITableView行时为什么会发生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8391504/