gpt4 book ai didi

ios - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update"尝试插入行时

转载 作者:行者123 更新时间:2023-11-28 06:10:02 25 4
gpt4 key购买 nike

我正在使用一个 UIViewController 和一个连接到另一个 UIViewControllerUITableView,我打算从中添加条目。它们通过 unwind segue 链接,但每当我尝试实际添加一个条目时,我都会得到这个:

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

这是我的:

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)")
}
}

@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()

}
}
}

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

return items.count

}

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

}

如何解决这个问题?

请原谅我,我对 Swift 还很陌生。 :)

最佳答案

如果您想使用 insertRowsAtIndexPaths:withRowAnimation:,您需要在更新数据数组后在 beginUpdatesendUpdates block 中执行此操作.根据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.

在调用 insertRowsAtIndexPaths:withRowAnimation: 之前更改数组是正确的,但是您需要使用批量更新 而不是 reloadData

文档链接中有一个完整的示例,您可以查看。

关于ios - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update"尝试插入行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47010202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com