gpt4 book ai didi

ios - 数学不相加 - UITableView 删除行错误

转载 作者:行者123 更新时间:2023-11-28 15:40:54 24 4
gpt4 key购买 nike

尝试从 UITableView 中删除一行时出现错误:

这是我的 TableView 的数据:

var tmpArray = ["test"];

下面是 TableView 获取行数的方法:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tmpArray.count;
}

这是我调用的删除行的方法的主体:

self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);
tmpArray.remove(at: 0);

这里是错误:

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 (1) must be equal to the number of rows contained in that section before the update (1), 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).'

奇怪的是,删除行之前的部分中应该有 1 行,而不是 2 行。

关于为什么会发生此错误的任何想法?

我查看了几篇文章,虽然相关,但此处提供的解决方案不起作用: UITableView Deleteing row error

更新当我单独调用删除命令时,错误仍然存​​在:

tmpArray.remove(at: 0);
self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);

错误:

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 (0) must be equal to the number of rows contained in that section before the update (0), 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).'

最佳答案

您真的需要在代码中使用这一行吗?

self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic);

难道不能像更新数据源和重新加载 UITableView 一样吗?喜欢:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {

//1. remove data from model
data.remove(at: indexPath.row)

//2. reload TableView
[tableView reloadData];

}
}

或者试试这个:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {

//1. remove data from model
data.remove(at: indexPath.row)

//2. remove row from view
tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)




}
}

关于ios - 数学不相加 - UITableView 删除行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43691398/

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