我正在加载我的 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/