我有一个 UITableView,我永远不想低于 1 个单元格:它是一个目录读数,如果目录中没有文件,它有一个单元格显示“无文件”。 (在编辑模式下,有一个用于创建文件的奖励单元格,因此编辑模式永远不会低于两个单元格。)
可能只是睡眠不足让我现在无法摆脱纸袋的束缚,但我总是犯这样的错误:
*** 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 (2) must be equal to
the number of sections contained in the table view before
the update (2), plus or minus the number of sections inserted
or deleted (1 inserted, 0 deleted).'
发生这种情况是因为我在删除最后一个文件之前先行添加“无文件”占位符。如果我在添加占位符之前删除最后一个文件单元格,则会发生类似的崩溃。无论哪种方式,单元格计数都会与 numberOfRowsInSection 的返回不同步,从而触发崩溃。
当然有针对这种情况的设计模式。给我提示一下吗?
按照下面的代码片段中所示的方式执行一些操作:
- 首先从数组中删除该行的数据
- 如果数组项尚未降至零,则从表中删除行
- 如果数组项已降至零,则重新加载表 - 注意:您的代码现在应该提供 1 行数,并配置第 0 行的单元格,以在调用 tableview 委托方法时显示“无文件”。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// First delete the row from the data source
[self deleteTableData: indexPath.row]; // method that deletes data from
if ([self.tableDataArray count] != 0) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
[self.tableView reloadData];
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)