菜鸟教程小白 发表于 2022-12-13 14:28:40

ios - TableView 上的无效更新


                                            <p><p>StackOverflow 的 friend 们好。</p>

<p>我的应用程序上有一个聊天屏幕,它根据数组的实际大小执行插入和删除。看这个:</p>

<pre><code>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(, withRowAnimation: .Right)
    }

    self.objects?.insert(object!, atIndex: 0)
    self.objectsTableView.insertRowsAtIndexPaths(, withRowAnimation: .Right)

    self.objectsTableView.endUpdates()

}
</code></pre>

<p>但是经过一些压力测试后,日志通知:</p>

<blockquote>
<p>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).</p>
</blockquote>

<p>我不知道发生了什么,只有在插入对象非常极端时才会发生这种情况,例如每 0.2 秒插入一个。 </p>

<p>有人知道我能做到吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>模型不匹配</strong></p>

<blockquote>
<p>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)</p>
</blockquote>

<p>对于通情达理的人来说,<code>UITableView</code> 认为你应该有 11 行:<br/>
<code>10</code> 更新前 + <code>1</code> 插入。 </p>

<blockquote>
<p>number of rows contained in an existing section after the update (1)</p>
</blockquote>

<p>...指的是 <code>numberOfRowsInSection</code> 正在为节 <code>0</code> 返回 <code>1</code>,这表明 <code>objects</code> 数组不同步,假设您使用以下内容:</p>



<pre><code>override func tableView(tableView: UITableView,
                        numberOfRowsInSection section: Int) -&gt; Int {
    return objects.count
}
</code></pre>

<hr/>

<p><strong>使用 <code>NSFetchedResultsController</code></strong></p>

<p>一个干净的解决方案是使用 <code>NSFetchedResultsController</code> 作为模型和 UI 之间的接口(interface)。它对样板代码进行了深入研究,是确保线程安全的绝佳平台。 <a href="https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/index.html" rel="noreferrer noopener nofollow">Documentation here</a> .</p>

<hr/>

<p><strong>注意:</strong></p>

<p>效果不错!单元格似乎在旋转到顶部。<br/>
我无法使用 <a href="https://gist.github.com/ppamorim/845517fed6bec0fd41ff" rel="noreferrer noopener nofollow">Gist</a> 打破它您制作,也没有安排多个并发测试。您的 <code>Object</code> 数组必须有<strong>恶意访问</strong>。</p>

<p><strong>演示</strong></p>

<p>这个简化版本有效。只需将 <code>doPlusAction</code>Hook 到按钮操作并观看它循环:</p>

<p> <a href="/image/33Uq4.gif" rel="noreferrer noopener nofollow"><img src="/image/33Uq4.gif" alt="neat effect"/></a> </p>



<pre><code>class TableViewController: UITableViewController {
    var objects: =
    var insertions = 5

    @IBAction func doPlusAction(sender: AnyObject) {
      tableView.beginUpdates()

      objects.removeAtIndex(4)
      tableView.deleteRowsAtIndexPaths(, withRowAnimation: .Right)
      objects.insert(insertions++, atIndex: 0)
      tableView.insertRowsAtIndexPaths(, 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()) { () -&gt; Void in
            self.doPlusAction(self)
      }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return objects.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier(&#34;reuseIdentifier&#34;, forIndexPath: indexPath)
      cell.textLabel!.text = &#34;Cell \(objects)&#34;
      return cell
    }
}
</code></pre>

<hr/></p>
                                   
                                                <p style="font-size: 20px;">关于ios - TableView 上的无效更新,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35441661/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35441661/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - TableView 上的无效更新