gpt4 book ai didi

ios - tableview commit editingStyle 正在删除错误的条目

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

以下代码删除了错误的条目。我有一个显示事件列表的数组。在调试器 indexPath.row 中显示 1(这是我选择删除的条目。但是当 View 刷新时,它删除了条目 4。

func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCellEditingStyle,
forRowAt indexPath: IndexPath) {
print(type(of: selectedRecipient))

var eventsOnArray = selectedRecipient?.events?.allObjects

guard let event = eventsOnArray?[indexPath.row] as? Event, editingStyle == .delete else {
return
}
managedContext.delete(event)
do {
try managedContext.save()
print("Deleted")
eventsOnArray?.remove(at: indexPath.row)
getEvents()
self.eventList.reloadData()
} catch let error as NSError {
print("Saving error: \(error), description: \(error.userInfo)")
}

}

最佳答案

我的猜测是您的数据源的顺序与您在我们看到的代码中使用的数组的顺序不同。

我基于这样一个事实,即您调用 events?.allObjects 这表明 events 是一个无序的 NSSet,调用它的 allObjects 会给您一个未定义顺序的数组。您需要一个数组而不是一个集合,这样您就可以通过代码的不同部分保证对象的排序顺序相同。

关于ios - tableview commit editingStyle 正在删除错误的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569456/

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