如何在swift中的UITableView中点击一行时获取联系号码(不是名称)

时间:2015-09-05 10:08:14

标签: 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])")
}

现在Outputtap name = John

我的Expected Outputtap name = John and number = ????????

我怎样才能得到它?

1 个答案:

答案 0 :(得分:0)

我不知道快速的语法,但我可以给你一个大致的想法。

首先,您只是在数组中添加名称。使用' name'创建一个字典。和'数字'键,为它们指定名称和编号,然后将其添加到阵列中。

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语法。