gpt4 book ai didi

ios - 核心数据: How can I update a TableView field from an alert?

转载 作者:行者123 更新时间:2023-11-30 11:56:42 25 4
gpt4 key购买 nike

我有一个使用 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这是一个作业)

提前致谢,我可以提供所需的任何其他信息。

最佳答案

在 TableView 的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
}

注意:这只是伪代码,可能包含语法错误

关于ios - 核心数据: How can I update a TableView field from an alert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com