首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >核心数据:如何从预警更新TableView字段?

核心数据:如何从预警更新TableView字段?
EN

Stack Overflow用户
提问于 2017-12-10 08:54:40
回答 1查看 176关注 0票数 0

我有一个使用核心数据创建的TableView。用户可以很容易地将新记录“添加”到TableView上-但是,我已经编写了一个更新函数,该函数显示一个更新警报,允许用户修改最近添加的字段。下面是"updateContact函数:

代码语言:javascript
复制
 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)
}

我相信我遇到的问题就是围绕着这段代码。

代码语言:javascript
复制
  // 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对象中存储用户修改的字段?

(附言:这是一项作业)

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

EN

回答 1

Stack Overflow用户

发布于 2017-12-11 14:51:58

在表视图的didSelectMethod上,我希望您的核心数据表的名称是ContactBusiness,

代码语言:javascript
复制
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
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47734830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档