简体   繁体   English

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

[英]attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update

There is strange thing with may tableView可能 tableView 有一些奇怪的事情

I have我有

In my delegate I have在我的代表中,我有

var editingIndexPath: IndexPath?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

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

    }

In my didSelect I have在我的 didSelect 中,我有

    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: [self.editingIndexPath!], with: .top)
            } else {
                editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
                if let editingIndexPath = editingIndexPath {
                    tableView.insertRows(at: [editingIndexPath], with: .top)
                }
            }
    }

The problem that it crashes after tableView.reloadData() with error tableView.reloadData() 报错后崩溃的问题

'attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update' '尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行'

I dont get it.我不明白。 TableView has exactly same number of rows it needs to perform insert. TableView 执行插入所需的行数完全相同。 I set property to nil and then reload table, by this action I reduce table's rows number to two.我将属性设置为 nil,然后重新加载表,通过此操作,我将表的行数减少到两行。 Then I again set editingIndexPath to not nil value signaling delegate method that it should use count + 1. But it fails same way.然后我再次将editingIndexPath设置为not nil value 信号委托方法,它应该使用count + 1。但它以同样的方式失败。

And what is also interesting that the same code but without reload before never fails还有什么有趣的,相同的代码但没有重新加载之前永远不会失败

            editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
            if let editingIndexPath = editingIndexPath {
                tableView.insertRows(at: [editingIndexPath], with: .top)
            }

What's happening here ?这里发生了什么事 ?

Table view has numberOfRowsInSection 3 for section 0(Row1, Row1, Row3).表视图的第 0 部分(Row1、Row1、Row3)的 numberOfRowsInSection 3。 On trying to insert Row4, which exceeds numberOfRowsInSection count, the above-mentioned error is thrown在尝试插入超过 numberOfRowsInSection 计数的 Row4 时,抛出上述错误

This may work better( Note: Clueless of logic intended)这可能会更好(注意:逻辑无能)

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     // better to check with datasource array elements count
     if indexPath.row + 1 < tableView.numberOfRows(inSection: indexPath.section) {
          self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
          tableView.insertRows(at: [self.editingIndexPath!], with: .top)
     } 
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “尝试将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 尝试将第0行插入第0部分,但更新后第0部分只有0行 - attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update Swift-“尝试将第0行插入第0节,但更新后第0节只有0行” - Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误 - “Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error 尝试插入行时“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行” - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" when trying to insert row 在表中插入一行时,“试图将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' when inserting a row into a table &#39;NSInternalInconsistencyException&#39;,原因:“试图将第0行插入第0节,但更新后第0节只有0行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 拖放时出现错误“尝试将第 4 行插入第 1 节,但更新后第 1 节中只有 4 行” - Getting an error "attempt to insert row 4 into section 1, but there are only 4 rows in section 1 after the update" while drop and drag &#39;NSInternalInconsistencyException&#39;,原因:“试图将第4行插入第0节,但更新后第0节只有4行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update' SwiftUI 应用程序因“尝试将第 2 行插入第 0 部分,但更新后第 0 部分中只有 2 行”错误而崩溃 - SwiftUI app crash with “attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM