【问题标题】:How to update CoreData data using Swift如何使用 Swift 更新 CoreData 数据
【发布时间】:2020-04-19 18:47:14
【问题描述】:

在 myscenario 中,我将 popupVC 文本字段 titledescript 值存储到 CoreData 中。曾经,将数据存储到 CoreData 中,然后将数据提取到 UITableView 中。我在editActionsForRowAt 中拥有的每个 Tableview 单元格都有编辑按钮。有一次,用户单击了我正在传递EditpopupVC.task = self.userData[indexPath.row] 的编辑按钮。在 EditpopupVC 中,我收到var task: NSManagedObject? = nil 之类的值。现在,如果用户单击更新按钮修改数据后,我将调用 func update(title: String, descript: String) { } 以获取传递的特定值更新。

编辑按钮单击以将数据从 ListViewController 传递到 EditpopupVC

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in
        print("Edit tapped")
        let task = self.userData[indexPath.row]
        let EditpopupVC = self.storyboard?.instantiateViewController(withIdentifier: "editviewcontroller") as! EditViewController
        EditpopupVC.titlename = "Edit"
        EditpopupVC.task = self.userData[indexPath.row]
        EditpopupVC.modalTransitionStyle   = .crossDissolve
        EditpopupVC.modalPresentationStyle = .overFullScreen
        self.present(EditpopupVC, animated: true, completion: nil)
    })
    return [editAction]
}

一旦用户修改文本数据然后更新按钮点击调用更新函数

update(title: Titletextfield.text ?? "Empty", descript: Descriptiontextview.text)

我在下面使用的CoreData更新函数

func update(title: String, descript: String) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let managedObjectContext = appDelegate.persistentContainer.viewContext
    task.title = title
    task.descript = descript
    do {
        try managedObjectContext.save()
    } catch {
        print("Could not save. \(error)")
    }
}

EditpopupVC.task = self.userData[indexPath.row] 这一行有助于将数据数组传递给EditpopupVC

【问题讨论】:

  • 你在哪里调用了这个update(indexPath:方法
  • @Anbu.Karthik 如果用户修改文本并单击更新按钮,我将调用 update(indexPath:
  • 您能否显示代码以及您在表格视图中调用编辑按钮目标的位置
  • @Anbu.Karthik 编辑按钮我放入editActionsForRowAtlet editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in 我打电话给let updateVC = self.storyboard?.instantiateViewController(withIdentifier: "updateVC")
  • @Anbu.Karthik 更新问题。请检查!

标签: ios swift uitableview core-data


【解决方案1】:

Core Data 对象是引用类型,您不需要将对象分配回数组。

发生崩溃是因为您没有在popupVC 中设置tasks,因此它保持为空并引发超出范围的异常。

在主控制器中用更具体的类型声明updateTitles

var updateTitles = [Myrecord]()

popupVC中删除tasks,声明任务为

var task : Myrecord!

并将update 替换为

func update(title: String, descript: String) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let managedObjectContext = appDelegate.persistentContainer.viewContext
    task.title = title
    task.descript = descript
    do {
        try managedObjectContext.save()
        self.dismiss(animated: true, completion: nil)
    } catch {
        print("Could not save. \(error)")
    }
}

不需要索引路径。

当控制器消失时重新加载表格视图。


旁注:

强制解开AppDelegate 很好。没有AppDelegate,应用程序甚至无法启动。

【讨论】:

  • 很大的帮助和非常有用的信息index path is not needed。太感谢了。我会尝试我的一面并在此处更新它。
  • 上述解决方案未覆盖。它正在创造新记录。
  • 此代码不会创建新记录。您在控制器的其他位置创建记录。
  • task.setValue(title, forKey: "title")task.setValue(descript, forKey: "descript") 得到错误Cannot invoke 'setValue' with an argument list of type '(String, forKey: String)'
  • 你应该使用NSManagedObject子类(实体类)和点符号task.title = "title")
猜你喜欢
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 2011-01-22
  • 2014-10-24
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多