简体   繁体   English

尝试插入行时“尝试将第 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

I'm using a UIViewController with a UITableView connected to another UIViewController where I intend to be able to add entries from.我正在使用UIViewControllerUITableView连接到另一个UIViewController我打算能够从中添加条目。 They are linked through an unwind segue, but whenever I try to actually add an entry, I get this:它们通过 unwind segue 链接,但是每当我尝试实际添加条目时,我都会得到以下信息:

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

This is what I have:这就是我所拥有的:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "") {

        case "AddItem":

        os_log("Adding a new item.", log: OSLog.default, type: .debug)

        case "ShowDetail":

            guard let DetailViewController = segue.destination as? ViewController else {

                fatalError("Unexpected destination: \(segue.destination)")
        }

            guard let selectedCell = sender as? TableViewCell else {

                fatalError("Unexpected sender: \(sender)")
        }

            guard let indexPath = tableView.indexPath(for: selectedCell) else {

                fatalError("The selected cell is not being displayed by the table")
        }

            let selectedItem = items[indexPath.row]

            DetailViewController.item = selectedItem

    default:

        fatalError("Unexpected Segue Identifier; \(segue.identifier)")
    }
}

and

@IBAction func unwindToList(sender: UIStoryboardSegue) {

    if let sourceViewController = sender.source as? ViewController, let item = sourceViewController.item {

        if let selectedIndexPath = tableView.indexPathForSelectedRow {

            items[selectedIndexPath.row] = item

            self.tableView.reloadRows(at: [selectedIndexPath], with: .none)

        }

        else {

            items.append(item)

            self.tableView.beginUpdates()

            self.tableView.insertRows(at: [IndexPath(row: items.count - 1, section: 0)], with: .automatic)

            self.tableView.endUpdates()

        }
    }
}

and

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

    return items.count

}

and

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifier = "TableViewCell"

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TableViewCell else {

        fatalError("The dequeued cell is not an instance of TableViewCell")

    }

    let item = items[indexPath.row]

    cell.labelName.text = item.name

    return cell

}

How can this be fixed?如何解决这个问题?

And forgive me, I am fairly new to Swift.原谅我,我对 Swift 还是很陌生。 :) :)

If you want to use insertRowsAtIndexPaths:withRowAnimation: you need to do it inside a beginUpdates and endUpdates block, after updating your data array.如果您想使用insertRowsAtIndexPaths:withRowAnimation:您需要在更新数据数组后在beginUpdatesendUpdates块中执行此操作。 According to Documentation :根据文档

To insert and delete a group of rows and sections in a table view, first prepare the array (or arrays) that are the source of data for the sections and rows.要在表视图中插入和删除一组行和节,首先准备作为节和行数据源的数组(或数组)。 After rows and sections are deleted and inserted, the resulting rows and sections are populated from this data store.在删除和插入行和节之后,由此数据存储填充生成的行和节。

You are doing correctly by changing the array before calling insertRowsAtIndexPaths:withRowAnimation: but you need to use batch updates instead of reloadData .您在调用insertRowsAtIndexPaths:withRowAnimation:之前更改数组是正确的,但您需要使用批量更新而不是reloadData

There is a complete example at the Documentation link that you can check.您可以查看文档链接中的完整示例。

Make sure the TableView's dataSource is connected (when using a storyboard or nib) or assigned programmatically.确保 TableView 的dataSource已连接(使用故事板或笔尖时)或以编程方式分配。 These errors also arise when trying to update a TableView or CollectionView with no dataSource assigned.当尝试更新没有分配dataSource的 TableView 或 CollectionView 时,也会出现这些错误。

暂无
暂无

声明:本站的技术帖子网页,遵循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 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行 - attempt to insert row 3 into section 0, but there are only 3 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' when inserting a row into a table 'NSInternalInconsistencyException',原因:“试图将第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 'NSInternalInconsistencyException',原因:“试图将第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