【问题标题】:How To Save Re-Ordered Tableview Cells To Core Data如何将重新排序的 Tableview 单元格保存到核心数据
【发布时间】: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


【解决方案1】:

这是我对这个问题的看法:

就像“vadian”所说,当你创建一个对象时,你会为附加到核心数据的数组添加一个属性,就像这样,

var itemsArray: [NSManagedObject] = []

save(sortOrder: itemsArray.count, item: itemTextField.text!) //Calling the save method wherever you need to, catching for example a UITextField user input

private func save(sortOrder: Int, item: String) {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let context = appDelegate.persistentContainer.viewContext
    guard let entity = NSEntityDescription.entity(forEntityName: "CoreDataBaseName", in: context) else { return }
    let itemManagedObject = NSManagedObject(entity: entity, insertInto: context)

    itemManagedObject.setValue(sortOrder, forKeyPath: "sortOrder")
    itemManagedObject.setValue(item, forKeyPath: "item")

    do {
        try context.save()
        itemsArray.append(itemManagedObject)

    } catch let error as NSError {
        print("Could not save item to Core Data: \(error)")
    }
}

那我们来看看tableView的moveRowAt方法,

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let context = appDelegate.persistentContainer.viewContext
    let movedItem = itemsArray[sourceIndexPath.row]

    itemsArray.remove(at: sourceIndexPath.row)
    itemsArray.insert(movedItem, at: destinationIndexPath.row)

    //This 'for-in loop' is at the heart of the solution
    for (i, item) in itemsArray.enumerated() {
        item.setValue(i, forKey: "sortOrder")
    }

    do {
        try context.save()
    } catch let error as NSError {
        print("Could not save/persist Core Data items in moveRowAt: \(error)")
    }
}

...在所有我们获取重新排列的顺序的最后,

private func fetch() {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let context = appDelegate.persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "CoreDataBaseName")
    let sortDescriptor = NSSortDescriptor(key: "sortOrder", ascending: true)

    fetchRequest.sortDescriptors = [sortDescriptor]

    do {
        itemsArray = try context.fetch(fetchRequest)
    } catch let error as NSError {
        print("Could not fetch Core Data entities: \(error)")
    }
}

有上百万种不同的方法可以改进这个想法(这总是受欢迎的),但这是一个解决方案的教学示例。

我希望它可以帮助那里的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    相关资源
    最近更新 更多