如何在 swift 的 UITableView 中点击一行获取联系人号码(不是姓名)
How to get contact Number (not name) on tapping a row in UITableView in swift
我正在用来自 phone 的联系人加载我的 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])")
}
现在 Output
是 tap name = John
我的Expected Output
是tap name = John and number = ????????
如何获取?
我不懂 swift 语法,但我可以给你一个大概的概念。
首先,您只是在数组中添加名称。使用 'name' 和 'number' 键创建一个字典,为它们分配名称和编号,然后将其添加到您的数组中。
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)
}
}
现在用相应的键在table中显示它:
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"])")
}
希望您掌握了通用的 idea.Someone 可以将其转换为 Swift 语法。
我正在用来自 phone 的联系人加载我的 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])")
}
现在 Output
是 tap name = John
我的Expected Output
是tap name = John and number = ????????
如何获取?
我不懂 swift 语法,但我可以给你一个大概的概念。
首先,您只是在数组中添加名称。使用 'name' 和 'number' 键创建一个字典,为它们分配名称和编号,然后将其添加到您的数组中。
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)
}
}
现在用相应的键在table中显示它:
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"])")
}
希望您掌握了通用的 idea.Someone 可以将其转换为 Swift 语法。