gpt4 book ai didi

swift - 删除最后一行并同时加载多行

转载 作者:行者123 更新时间:2023-11-30 11:53:15 27 4
gpt4 key购买 nike

当我从表格 View 中删除最后一行时:

tableView.deleteRows(at: [indexPath], with: .middle)

并将我的表的数据源更改为另一个数组:

if orders.isEmpty {
return ABCDArray.count
} else {
return orders.count
}

它给了我这个错误:

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 (20) 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).'

我该如何解决这个问题?更改数据源有什么问题?

最佳答案

在调用 tableView.deleteRows(at: [indexPath], with: .middle) 之前,您需要从数据数组中删除该对象。因此,您的代码应如下所示:

// Editing of rows is enabled
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
//when delete is tapped
orders.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .middle)
}
}

关于swift - 删除最后一行并同时加载多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123727/

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