核心数据:如何从警报中更新 TableView 字段?
Posted
技术标签:
【中文标题】核心数据:如何从警报中更新 TableView 字段?【英文标题】:Core Data: How can I update a TableView field from an alert? 【发布时间】:2017-12-10 00:54:40 【问题描述】:我有一个使用 Core Data 创建的 TableView。用户可以轻松地将新记录“添加”到 TableView 中 - 但是,我编写了一个更新函数,显示更新警报,允许用户修改最近添加的字段。这里是“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 这是一个作业)
提前致谢,我可以提供所需的任何其他信息。
【问题讨论】:
您需要将现有的 NSManagedObject 联系人传递给此函数,然后简单地更新该对象的属性。 你有这方面的例子吗?我明白,但我想不通。 【参考方案1】:在表格视图的 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
注意:这只是伪代码,可能包含语法错误
【讨论】:
以上是关于核心数据:如何从警报中更新 TableView 字段?的主要内容,如果未能解决你的问题,请参考以下文章
从 tableView 上的核心数据填充数据时如何避免行重复