【发布时间】:2015-09-07 14:21:09
【问题描述】:
我正在尝试重新加载 tableView 部分,而不是重新加载整个 tableview,因为我在标题部分中有一个文本字段,当我调用 self.tableView.reloadData() 时,我的键盘关闭了。
我已经尝试了下面的代码,但我收到了这个错误Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 5 from section 2 which only contains 4 rows before the update' 那么请问我的问题在哪里?
let newCount = self.placeArray.count
var newIndexPaths = NSMutableArray(capacity: newCount)
for var i = 0 ; i < newCount ; i++ {
var indexPath = NSIndexPath(forRow: i, inSection: 2)
newIndexPaths.addObject(indexPath)
}
self.tableView.beginUpdates()
self.tableView.reloadRowsAtIndexPaths(newIndexPaths as [AnyObject], withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()
【问题讨论】:
-
这样的代码容易造成不一致,不要害怕调用datasource方法获取行数。
-
@A-Live 对不起,我不明白怎么做?
-
使用
numberOfRowsInSection而不是从那里复制逻辑,这样数字必须始终正确。您没有显示或提及对行数的任何修改,所以我假设您没有这样做,否则您必须根据获取数字和修改数据源的时间来相应地更改数字。 -
我认为你给了我解决方案,但我没有得到你。我正在用新数组更新
numberOfRows。 @A-Live -
您尝试更新的行数多于实际行数,表格视图假定您尝试删除一行 - 这是由于您在计算行数时的错误造成的。
标签: ios swift uitableview