我有一个使用核心数据创建的表观视图。用户可以轻松地"添加"新记录 - 但是,我编写了一个更新功能,该功能显示了一个更新警报,该功能允许用户修改最近添加的字段。这是" UpdateContact函数:
func updateBusinessContact(name: String, email: String, phone: String, company: String)
{
// create an Alert with a textFields for all ContactBusiness fields
let alertController = UIAlertController(title: "Update Contact Business",
message: "",
preferredStyle: UIAlertControllerStyle.alert)
// add the textField to the Alert. Create a closuer to handle the configuration
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = name
textField.isUserInteractionEnabled = false
textField.keyboardType=UIKeyboardType.namePhonePad
// textField.secureTextEntry = true // password entry
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = email
textField.keyboardType=UIKeyboardType.emailAddress
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = phone
textField.keyboardType=UIKeyboardType.phonePad
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.placeholder="Street"
textField.keyboardType=UIKeyboardType.default
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.placeholder="City"
textField.keyboardType=UIKeyboardType.default
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = "oh"
textField.keyboardType=UIKeyboardType.default
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = "44121"
textField.keyboardType=UIKeyboardType.numberPad
})
alertController.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = company
textField.keyboardType=UIKeyboardType.default
})
// create a default action for the Alert
let defaultAction = UIAlertAction(
title: "Ok",
style: UIAlertActionStyle.default,
handler: {(alertAction: UIAlertAction!) in
/// get the input from the alert controller
let name: String = (alertController.textFields![0]).text!
let email: String = (alertController.textFields![1]).text!
let phone: String = (alertController.textFields![2]).text!
let street: String = (alertController.textFields![3] ).text!
let city: String = (alertController.textFields![4] ).text!
let state: String = (alertController.textFields![5] ).text!
let zip: String = (alertController.textFields![6] ).text!
let company: String = (alertController.textFields![7]).text!
// add Contact to the managedOBject
_ = ContactBusiness(managedObjectContext: self.managedObjectContext,
name: name, email: email, phone: phone, street:street, city: city, state: state, zip: zip, company: company)
// save the managedObject
CoreDataHelper.addContactBusiness(managedObjectContext: self.managedObjectContext)
// get all Contacts from CoreData
self.cbArray = CoreDataHelper.getAllContactBusiness(managedObjectContext: self.managedObjectContext)
// reload the data into the TableView
self.tvContacts.reloadData()
})
let cancelAction = UIAlertAction(
title: "Cancel",
style: UIAlertActionStyle.cancel,
handler:nil)
// add the action to the Alert
alertController.addAction(defaultAction)
alertController.addAction(cancelAction)
// display the Alert
present(alertController, animated: true, completion: nil)
}
我相信我遇到的问题是在这个代码块
的某个地方旋转 // add Contact to the managedOBject
_ = ContactBusiness(managedObjectContext: self.managedObjectContext,
name: name, email: email, phone: phone, street:street, city: city, state: state, zip: zip, company: company)
这有效,但是它没有更新预现实的TableView条目 - 它只是在表中添加了另一个带有更新信息的对象。
如何将用户修改的字段存储在ContactBusiness对象中?
(P.S这是用于分配的)
预先感谢,我可以提供任何其他所需的信息。
table View的didSelectMethod,在这里,我希望您的核心数据表的名称是ContactBusiness,如果有任何其他变化,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentContactBusiness = self.cbArray[indexPath.row]
//you can call function updateBusinessContact here
}
//Updated Function
func updateBusinessContact(name: String, email: String, phone: String, company: String, currentContactBusiness : ContactBusiness)
{
//update the details of currentContactBusiness
}
注意:这只是伪代码,可能包含语法错误