如何将联系人姓名添加到表格视图单元格?

时间:2015-04-26 00:30:52

标签: ios uitableview swift

嘿所以我有一个包含5个单元格的表格视图,每当选择其中一个单元格时,会弹出一个ABPeoplePickerNavigationController,用户可以选择一个联系人。如何使所选联系人的姓名成为单元格标签的文本?我现在已经在互联网上搜索了大约2个小时,我无法为我的生活找到任何东西。 :(最近的事情,我发现使用数组存储动态单元格中的多个联系人,但我有静态单元格...任何帮助或提示将不胜感激。 这是我到目前为止所做的:

override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let picker = ABPeoplePickerNavigationController()
        picker.peoplePickerDelegate = self
        presentViewController(picker, animated: true, completion: nil)
    }

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {

        let multiValue: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()
        let index = ABMultiValueGetIndexForIdentifier(multiValue, identifier)
        let contactInfo = ABMultiValueCopyValueAtIndex(multiValue, index).takeRetainedValue() as! String

        let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String

    }

    func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {

        peoplePickerNavigationController(peoplePicker, didSelectPerson: person, property: property, identifier: identifier)

        peoplePicker.dismissViewControllerAnimated(true, completion: nil)

        return false;
    }

    func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController!) {
        peoplePicker.dismissViewControllerAnimated(true, completion: nil)
    }

1 个答案:

答案 0 :(得分:1)

didSelectRowAtIndexPath中,创建对所选行的引用。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    // Create a property for the selected cell, and set it when the row is selected
    self.selectedCell = tableView.cellForRowAtIndexPath(indexPath)

    let picker = ABPeoplePickerNavigationController()
    picker.peoplePickerDelegate = self
    presentViewController(picker, animated: true, completion: nil)

}

ABPeoplePickerNavigationController中选择联系人后,将所选单元格的文本设置为firstName

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool {

    peoplePickerNavigationController(peoplePicker, didSelectPerson: person, property: property, identifier: identifier)

    let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String

    // Set the text of the selected cell
    self.selectedCell.textLabel.text = firstName

    // You might have to reload the tableView data on completion to reflect the change
    peoplePicker.dismissViewControllerAnimated(true, completion: {
        self.tableView.reloadData()
    })

    return false;
}

此外,我相信自iOS 8.0以来已弃用shouldContinueAfterSelectingPerson:,因此如果您定位的是8.0及以上,则应该只使用didSelectPerson:

Apple docs供参考。