嗨,StackOverflow 的 friend 们。
我的应用程序上有一个聊天屏幕,它根据数组的实际大小执行插入和删除操作。看这个:
func addObject(object: Object?) {
if comments == nil || object == nil || object?.something == nil || object?.anything == nil {
return
}
self.objectsTableView.beginUpdates()
if self.objects!.count == 10 {
self.objects?.removeAtIndex(9)
self.objectsTableView.deleteRowsAtIndexPaths([NSIndexPath(forRow : 9, inSection: 0)], withRowAnimation: .Right)
}
self.objects?.insert(object!, atIndex: 0)
self.objectsTableView.insertRowsAtIndexPaths([NSIndexPath(forRow : 0, inSection: 0)], withRowAnimation: .Right)
self.objectsTableView.endUpdates()
}
但经过一些压力测试后,日志通知:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
我不知道发生了什么,只有当对象插入非常极端时才会发生这种情况,比如每 0.2 秒一个。
有人知道我能做什么吗?
最佳答案
模型不匹配
The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted)
用通俗易懂的话来说,UITableView
认为您应该有 11 行:
10
更新前 + 1
插入。
number of rows contained in an existing section after the update (1)
...指的是 numberOfRowsInSection
正在为 0
部分返回 1
,这表明 objects
数组不同步,假设您使用如下内容:
override func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return objects.count
}
使用NSFetchedResultsController
一个干净的解决方案是使用 NSFetchedResultsController
作为模型和 UI 之间的接口(interface)。它对样板代码进行了深入研究,是确保线程安全的绝佳平台。 Documentation here .
注意:
效果不错!细胞似乎在旋转到顶部。
我无法使用 Gist 破坏它你生产,也没有安排多个并发测试。必须存在对您的 Object
数组的恶意访问。
演示
这个简化版本有效。只需将 doPlusAction
挂接到按钮操作并观察它循环:
class TableViewController: UITableViewController {
var objects:[Int] = [0,1,2,3,4]
var insertions = 5
@IBAction func doPlusAction(sender: AnyObject) {
tableView.beginUpdates()
objects.removeAtIndex(4)
tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: 4, inSection: 0)], withRowAnimation: .Right)
objects.insert(insertions++, atIndex: 0)
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Right)
tableView.endUpdates()
let delay = 0.1 * Double(NSEC_PER_SEC) //happens the same with this too, when reach 100-150 items
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
self.doPlusAction(self)
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.textLabel!.text = "Cell \(objects[indexPath.row])"
return cell
}
}
关于ios - TableView 上的更新无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35441661/