ios - TableView 上的更新无效

标签 ios uitableview tableviewcell

嗨,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 挂接到按钮操作并观察它循环:

neat effect

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/

相关文章:

ios - 如果我使用仅自 iOS 10 起可用的内容,这是否意味着我无法在 iOS 9 上安装该应用程序?

iphone - 使用AVAudioPlayer,我的声音(偶尔!)听起来很奇怪

ios - swift : How to avoid duplicate entries in CoreData for implementation with JSON array?

ios - 如何在 IOS 中删除没有动画的 UITableView 中的行

Swift: bool 如果为真,则不显示图像

ios - SwiftUI 中的 Gridview

c# - 未调用子类 UIView 的绘制方法(另一个子类 UIView 的子类)

objective-c - 通过多个函数为对象属性分配值

ios - 想要在静态 TableViewCells 的 Touch up 内部触发 IBAction

ios - 自定义 UITableViewCell,单元格中的按钮不起作用