ios - 如何在快速点击 UITableView 中的一行时获取联系号码(而不是姓名)

标签 ios swift uitableview abaddressbook

我正在加载我的 tblView 中的联系人信息。在点击该 tblView 中的一行时,我可以获得点击它的名称。

我想知道如何通过点击获取联系电话。

经过许可,我将联系人存储在数组 arrContatcs 中:

let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
    for record in allContacts {
        let currentContact: ABRecordRef = record
        let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue()
            as? String
        if(currentContactName != nil) {
            self.arrContacts.append(currentContactName! as String)
        }
     }

从这里加载 tblView 中的联系人:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
    var cell:FriendsCustomCell = self.tblView.dequeueReusableCellWithIdentifier("SimpleTableViewIdentifier") as! FriendsCustomCell
    cell.tag = indexPath.row;
    cell.lblName!.text = self.arrContacts[indexPath.row] as? String
}

正在打印此名称:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = NSIndexPath(forRow: indexPath.row, inSection: 0)
    println("tap name = \(self.arrContacts[indexPath.row])")
}

现在输出tap name = John

我的预期输出tap name = John and number = ?????????

我怎样才能得到它?

最佳答案

我不知道 swift 语法,但我可以给你一个总体思路。

首先,您只是在数组中添加名称。创建一个包含“名称”和“数字”键的字典,为它们分配名称和数字,然后将其添加到数组中。

let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
for record in allContacts {
    let currentContact: ABRecordRef = record
    let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue()
        as? String
    let currentContactNumber = ABRecordCopyCompositeNumber(currentContact)?.takeRetainedValue()
        as? String

    if(currentContactName != nil && currentContactNumber!=nil) {
    //Create an array here with name and number. Objective-C code
        NSDictionary *dict = [[dict alloc]init];
        [dict setValue:currentContactName forKey:@"name"];
        [dict setValue:currentContactNumber forKey:@"number"];

       //Syntax can be wrong here. add dictionary to array
       self.arrContacts.append(dict! as Dictionary)
    }
 }

现在将其显示在带有相应键的表格中:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
    var cell:FriendsCustomCell = self.tblView.dequeueReusableCellWithIdentifier("SimpleTableViewIdentifier") as! FriendsCustomCell
    cell.tag = indexPath.row;
//Sorry for syntax
    cell.lblName!.text = self.arrContacts[[indexPath.row] ValueForKey@"name"] as? String
    cell.lblNumber!.text = self.arrContacts[[indexPath.row] ValueForKey@"number"] as? String
}

现在点击即可打印姓名和号码

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = NSIndexPath(forRow: indexPath.row, inSection: 0)
    println("tap name = \(self.arrContacts[indexPath.row] ValueForKey:@"name"])")
    println("tap Number = \(self.arrContacts[indexPath.row] ValueForKey:@"number"])")
}

希望您能了解总体思路。有人可能会将其转换为 Swift 语法。

关于ios - 如何在快速点击 UITableView 中的一行时获取联系号码(而不是姓名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32411854/

相关文章:

ios - 如何从 AppStore 中删除对 iPad 的支持

iOS Firebase 崩溃报告设置时崩溃

ios - setEnabledTextCheckingTypes 无法识别的选择器发送到实例

ios - 如何呈现 UICollectionViewController?

iphone - 优秀的 UITableView 设计图库

iOS - UITableViewCell 中 UIButton 的延迟 "Touch Down"事件

ios - 如何处理缺少键的字典

iphone - 为什么此代码会引发 "CoreData: error: (19) PRIMARY KEY must be unique"错误?

objective-c - 如何删除具有权限的文件

ios - 获取选中标记的行并发送到控制台