菜鸟教程小白 发表于 2022-12-11 19:08:11

ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行


                                            <p><p>may tableView 有点奇怪</p>

<p>我有</p>

<p>在我的委托(delegate)中,我有</p>

<pre><code>var editingIndexPath: IndexPath?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {

      if let editingIndexPath = editingIndexPath {
            return datasource.count + 1
      } else {
            return datasource.count
      }

    }
</code></pre>

<p>在我的 didSelect 我有</p>

<pre><code>    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            if let oldEditingIndexPath = editingIndexPath {
                  self.editingIndexPath = nil
                  tableView.reloadData()
                  self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
                  tableView.insertRows(at: , with: .top)
            } else {
                editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
                if let editingIndexPath = editingIndexPath {
                  tableView.insertRows(at: , with: .top)
                }
            }
    }
</code></pre>

<p>tableView.reloadData() 报错后崩溃的问题</p>

<blockquote>
<p>&#39;attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update&#39;</p>
</blockquote>

<p>我不明白。 TableView 具有执行插入所需的完全相同的行数。我将属性设置为 nil 然后重新加载表,通过这个操作我将表的行数减少到两个。然后我再次将 <code>editingIndexPath</code> 设置为非零值信号委托(delegate)方法,它应该使用 count + 1。但它以同样的方式失败。</p>

<p>同样有趣的是,相同的代码但没有重新加载之前永远不会失败</p>

<pre><code>            editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
            if let editingIndexPath = editingIndexPath {
                tableView.insertRows(at: , with: .top)
            }
</code></pre>

<p>这里发生了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>表格 View 的第 0 部分(第 1 行、第 1 行、第 3 行)有 numberOfRowsInSection 3。在尝试插入超过 numberOfRowsInSection 计数的 Row4 时,抛出上述错误</p>

<p>这可能会更好(注意:毫无逻辑的意图)</p>

<pre><code>func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   // better to check with datasource array elements count
   if indexPath.row + 1 &lt; tableView.numberOfRows(inSection: indexPath.section) {
          self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
          tableView.insertRows(at: , with: .top)
   }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46975985/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46975985/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行