我有一个tableView,其中显示了我在另一个类中创建的0个对象。对象存储在数组中。
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])
}
}
这个方法实际上是完美的,单元格被删除。但每次我返回此视图时,删除的单元格都会重新回到表中。
我是swift的新手,但我认为这是因为字典中的单元格被删除了,而whiskyArray中的对象没有被删除。我试图删除whiskyArray中的对象:
whiskyArray.removeAtIndex(indexPath.row)
但这样一来,whiskyArray中的任何对象都会被删除,而不是我想删除的对象。因此,如何通过删除tableView中的单元格来访问要在whiskyArray中删除的正确对象。我发现了很多关于删除tableViewCells的内容,但是我不知道如何删除数组中的有意义的对象。有人能帮我吗?
最佳答案
您需要确保它实际上是从数据中删除的。当您从数组中删除它时,它只是一个本地副本,当您离开视图并返回时,我的猜测是,您正在将数据重新加载到数组中,实际上重新开始。它与UITableView代码无关。