我有一张表显示客户愿望清单上的项目。他们可以使用“Move to Collection”按钮将希望的项目从列表中移出并进入他们的集合。此按钮是一个布尔值。
如何将该值从true更改为false?
这是我的Swift代码:
let moveAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Move to Collection",handler: { (action, indexPath) -> Void in
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
//Change the boolean of the collection to false.
let context = appDelegate.persistentContainer.viewContext
let friendToDelete = self.fetchResultController.object(at: indexPath)
context.delete(friendToDelete)
appDelegate.saveContext()
}
})
答案 0 :(得分:0)
let moveAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Move to Collection",handler: { (action, indexPath) -> Void in
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
let managedObject = self.fetchResultController.object(at: indexPath)
// Change the boolean of the collection to false.
managedObject.setValue(false, forKey: "yourKey") // managedObject.wish = false
//...
if context.hasChanges {
do {
try context.save()
} catch {
print(error.localizedDescription)
}
}
}
})
因此,请从indexPath
获取托管对象。并将新值提供给托管对象,就是这样。只需保存更改。让我知道是否可行。