【发布时间】:2018-12-30 05:06:28
【问题描述】:
所以我有我的tableview,当我重新排序我的行时,它会更新tableview 和所有内容,但它不会保存到core data。
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let movedCampSites = itemName[sourceIndexPath.item]
itemName.remove(at: sourceIndexPath.item)
itemName.insert(movedCampSites, at: destinationIndexPath.item)
context.delete(itemName[sourceIndexPath.row])
context.insert(itemName[destinationIndexPath.row])
do
{
try context.save()
}
catch
{
print("Could not move rows")
}
}
在context.insert(itemName[destinationIndexPath.row]) 之前一切正常,例如,如果我将其注释掉并运行它,它将移动行并删除行并保存到core data,但context.insert 不会保存新行位置.由于某种原因,它正在运行我的 catch 块,因为我收到错误无法移动行。
这是控制台中的一些错误。
2018-07-22 09:27:01.884925-0700 搜索栏核心数据[67957:5383630] [错误] 错误:(1555)唯一约束失败:ZTITLE.Z_PK CoreData:错误:(1555)唯一约束失败:ZTITLE.Z_PK 无法移动行
提前致谢。
【问题讨论】:
-
从Core Data的角度来看没有区别,因为它保存了无序的对象。
-
好的,我怎么能改变这个,这样才有效?
-
如果您需要特定顺序,请使用或添加可以按此顺序排序的 Core Data 属性(例如 Int32 索引)
-
如果这个答案是正确的,我会很感激一个绿色的复选标记 =D
标签: swift core-data tableview reorderlist