gpt4 book ai didi

swift - 使用 commitEditingStyle 删除行和部分

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

我有一个 tableView,其中显示了我在另一个类中创建的 0bjects。对象存储在数组中。

var whiskyArray = [WhiskyBuilder]()

在 whiskyArray 之外,我创建了一个字典来将数据移动到 tableView 中。字典以首字母为键,对象的名称为值:

var alphabetizedWhiskies = [String: [WhiskyBuilder]]()

现在我想从 tableView 中删除带有 commitEditingStyle: 的单元格,我使用以下方法执行此操作:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 

if editingStyle == UITableViewCellEditingStyle.Delete {

alphabetizedWhiskies[Array(alphabetizedWhiskies.keys.sort())[indexPath.section]]!.removeAtIndex(indexPath.row)

if alphabetizedWhiskies[Array(alphabetizedWhiskies.keys.sort())[indexPath.section]]!.count == 0 {

alphabetizedWhiskies.removeValueForKey(Array(alphabetizedWhiskies.keys.sort())[indexPath.section])

tableView.deleteSections(NSIndexSet(index:indexPath.section), withRowAnimation: .Fade)

}else {

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

alphabetizedWhiskies.removeValueForKey(Array(alphabetizedWhiskies.keys.sort())[indexPath.row])

}
}

这个方法真的很完美,单元格被删除了。但是每次我返回到这个 View 时,删除的单元格又回到了表格中。我是 swift 的新手,但我认为发生这种情况是因为单元格在字典中被删除,但 whiskyArray 中的对象却没有。我试图删除 whiskyArray 中的对象:

whiskyArray.removeAtIndex(indexPath.row)

但是有了这个,whiskyArray 中的任何对象都被删除了,但不是我想删除的对象。那么如何通过删除 tableView 中的单元格来访问要在 whiskyArray 中删除的正确对象。我找到了很多关于删除 tableViewCells 的内容,但我没有找到如何删除 Array 中的意义对象。谁能帮帮我?

最佳答案

您需要确保它确实已从数据中删除。当您从数组中删除它时,它只是一个本地副本,当您离开 View 并返回时,我的猜测是您正在将数据重新加载到数组中,本质上是重新开始。它与您的 UITableView 代码无关。

关于swift - 使用 commitEditingStyle 删除行和部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39004717/

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