gpt4 book ai didi

swift - 无效更新 : invalid number of rows in section 0, 更新后现有节中包含的行数

转载 作者:行者123 更新时间:2023-11-28 13:49:17 27 4
gpt4 key购买 nike

当我滑动并删除 UITableCell 时,我不断收到此错误

Terminating app due to uncaught exception NSInternalInconsistencyException, reason: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (11) must be equal to the number of rows contained in that section before the update (11), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = deleteAction(at: indexPath)
tableView.reloadData()
print(listOfCoins)
print(listOfCoins.count)
return UISwipeActionsConfiguration(actions: [delete])
}

func deleteAction(at indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, nil) in
self.listOfCoins.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.reloadData()
}
action.title = "Delete"
action.backgroundColor = .red
return action
}

我已经尝试删除允许应用程序运行的“self.tableView.deleteRows”并从我的源中删除 indexPath,但我不知道为什么当“tableView.reloadData()”时单元格不会消失' 之后被调用。

enter image description here

最佳答案

你只需要

self.listOfCoins.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)

所以删除这个

let delete = deleteAction(at: indexPath)
tableView.reloadData() // remove this line

self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.reloadData() // remove this line

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = deleteAction(at: indexPath)
print(listOfCoins)
print(listOfCoins.count)
return UISwipeActionsConfiguration(actions: [delete])
}

func deleteAction(at indexPath: IndexPath) -> UIContextualAction {
let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, nil) in
self.listOfCoins.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
action.title = "Delete"
action.backgroundColor = .red
return action
}

关于swift - 无效更新 : invalid number of rows in section 0, 更新后现有节中包含的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946345/

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