gpt4 book ai didi

swift - 从 TableViewController 中删除单元格

转载 作者:行者123 更新时间:2023-11-30 10:12:01 25 4
gpt4 key购买 nike

我想从 tableViewController 中删除单元格,但问题是删除下一个单元格例子 :数组:[1,2,3,4,5]当我从表中删除 2 时,代码会自动从数据库中删除 3

这是我的代码:

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
array_prodacts.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! TableViewCell2 }}

最佳答案

问题出在这里 array_prodacts.removeAtIndex(indexPath.row) 因为 tableview 是从零开始的索引,所以元素 1 的 indexPath你的数组是 0,2 是 1,3 是 2,4 是 3,5 是 4。因此,删除索引 2 反射(reflect)了删除数组中的值 3(具有索引 2)

关于swift - 从 TableViewController 中删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32454921/

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