我会让用户通过点击表格单元格来编辑数据。用户将选择表格单元格,无论他们想要修改哪个数据,新的VC" UpdateDataViewController"将出现以前选择的数据。用户可以修改数据并点击保存按钮将编辑后的数据保存在数据库表中。
这是我的代码:
//Code to pass the data in selected row
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "update" {
var path = self.tableView.indexPathForSelectedRow
let nav = segue.destination as! UINavigationController
let updateViewController = nav.topViewController as! UpdateDataViewController
updateViewController.index = path?.row
updateViewController.editPeopleList = peopleList[(path?.row)!].name
// let updatePersonModel = PersonModel()
// updateViewController.editPeopleList = [updatePersonModel.name!]
}
}
UpdateDataViewController.swift
// code to select the row and enable to edit touching any place of text field
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 && indexPath.row == 0 {
updateDataTextField.becomeFirstResponder()
}
tableView.deselectRow(at: indexPath, animated: true)
}
所需的变量:
var index:Int?
var editPeopleList:String!
var updatePeopleList: String?
override func viewDidLoad() {
super.viewDidLoad()
updateDataTextField.text = editPeopleList
}
// after editing data is replaced here:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "updateData" {
updatePeopleList = updateDataTextField.text
}
}
使用unwind segue更新核心数据的代码:
//code for unwind segue
@IBAction func updateDataToViewController(segue: UIStoryboardSegue) {
let updatePeopleData = segue.source as! UpdateDataViewController
let index = updatePeopleData.index
// let dataToUpdate = peopleList[index!].name
let personData = updatePeopleData.updatePeopleList
let updatePersonName = PersonModel()
updatePersonName.name = personData
let path = self.tableView.indexPathForSelectedRow
let selectedPersonData = peopleList[(path?.row)!]
PersonServices.sharedInstance.updatePerson(updatePersonName: updatePersonName, selectedPersonData: selectedPersonData )
// updatePersonModel.name = name
//peopleList[index!] = updatePeopleData.
tableView.reloadData()
}
这是我用于更新核心数据中数据的更新功能:
func updatePerson(updatePersonName: PersonModel , selectedPersonData: PersonModel) {
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersonEntity)
let updatepredicates = NSPredicate(format: "name == %@" , selectedPersonData.name! )
print("data to update: \(updatepredicates)")
print("updated data: \(String(describing: updatePersonName.name))")
fetchRequest.predicate = updatepredicates
print("after edit: \(fetchRequest)")
var updatePersonModels = [PersonModel]();
do {
if let person = try
self.managedObjectContext.fetch(fetchRequest).first as? Person {
let newPersondata = NSEntityDescription.insertNewObject(forEntityName: PersonEntity, into: managedObjectContext)
newPersondata.setValue("updatePersonName.name", forKey: person.name!)
do {
try self.managedObjectContext.save()
updatePersonModels.append(PersonModel(person: person))
} catch let error as NSError {
print("could not save. \(error), \(error.userInfo)")
}
}
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
但是我的应用程序崩溃了
newPersondata.setValue("updatePersonName.name", forKey: person.name!)
在updatePerson函数中。
错误是:
因未捕获的异常终止应用&#39; NSUnknownKeyException&#39;, 原因:&#39; [setValue:forUndefinedKey:] :. 实体人员不是密钥值编码兼容的密钥&#34; Umar&#34;。&#39;
答案 0 :(得分:2)
最后我做了,Ahumdulillah。
起初我改变了我的&#34; updatePerson&#34;功能如下:
func updatePerson(updatePersonName: PersonModel , selectedPersonData: PersonModel) {
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: PersonEntity)
let updatepredicates = NSPredicate(format: "name == %@" , selectedPersonData.name! )
fetchRequest.predicate = updatepredicates
var updatePersonModels = [PersonModel]();
do {
if let person = try self.managedObjectContext.fetch(fetchRequest).first as? Person {
person.name = updatePersonName.name
print("person name is: \(person.name)")
do {
try self.managedObjectContext.save()
updatePersonModels.append(PersonModel(person: person))
} catch let error as NSError {
print("could not save. \(error), \(error.userInfo)")
}
}
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
然后,在&#34; updateDataToViewController&#34;展开segue函数:我刚刚将编辑过的数据放入选定的索引中。
@IBAction func updateDataToViewController(segue: UIStoryboardSegue) {
let updatePeopleData = segue.source as! UpdateDataViewController
let index = updatePeopleData.index
let dataToUpdate = peopleList[index!].name
let personData = updatePeopleData.updatePeopleList
let updatePersonName = PersonModel()
updatePersonName.name = personData
let selectedPersonData = peopleList[index!]
PersonServices.sharedInstance.updatePerson(updatePersonName: updatePersonName, selectedPersonData: selectedPersonData )
peopleList[(path?.row)!] = updatePersonName
tableView.reloadData()
}
多数民众赞成.....