所以这是我的代码:(我想重新排序表并更新核心数据):
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
let entity = NSEntityDescription.entityForName(entity, inManagedObjectContext: managedContext)
let entityObject = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
entityObject.setValue(content, forKey: key)
self.managedContext.insertObject(entityObject, atIndex: 0) //ERROR HERE
do {
try managedContext.save()
} catch let error as NSError {
}
}
我见过类似的代码 here但为什么我的不工作?谢谢!
最佳答案
您提供的链接与您在此处所做的不同。他在那里所做的是将对象移除和插入到 Playlist
数组中。
如果你真的想重新排序表格并相应地更新 Core Data,你可能想在你的 Core Data 模型中添加一个 index
字段,并每次用单元格的索引更新它移动单元格。
因此您可以按照 index
字段的顺序将数据填充到 TableView 中,并使单元格顺序与数据模型保持同步。
关于ios - managedContext.insertObject 在 Swift 中调用 `atIndex` 的额外参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34811508/