Swift TableView 地址簿数组错误
Swift TableView AddressBook Array Error
我有 tableview,里面有地址簿项目(firstName,lastName,phone)所以我的单元格只显示我设备的最后一个 属性 乘以 numberOfRowsInSection
但在那之后我有 println()
并且它给了我完整的联系人姓名列表。我不明白为什么这一切都会发生,请帮助我。回顾一下:在 Xcode 中,我有完整的联系人列表,但在设备上我只有最后一个和 5 次。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
let allPeople = ABAddressBookCopyArrayOfAllPeople(AddressBookForVC).takeRetainedValue() as NSArray
for person: ABRecordRef in allPeople{
let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as! String? ?? ""
let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as! String? ?? ""
let phones: ABMultiValueRef = ABRecordCopyValue(person,kABPersonPhoneProperty).takeRetainedValue()
let countOfPhones = ABMultiValueGetCount(phones)
for index in 0..<countOfPhones{
let phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as! String
// println("\(firstName) \(lastName) \(phone)")
cell.textLabel?.text = firstName
println(firstName)
}
}
// cell.textLabel?.text = swiftBlogs[row]
return cell
}
cellForRowAtIndexPath
对 table 中的每一行调用一次。每次调用 cellForRowAtIndexPath
时,您每次都在提取和遍历所有联系人的数组。
copying/looping 所有联系人的代码在 cellForRowAtIndexPath
中都没有意义。通常你会更早地提取所有这些信息(例如在 viewDidLoad
中),然后填充一个数组一次。然后 cellForRowAtIndexPath
将简单地在该数组中查找数据,而不是每次都返回地址簿并重新提取所有联系人。
我有 tableview,里面有地址簿项目(firstName,lastName,phone)所以我的单元格只显示我设备的最后一个 属性 乘以 numberOfRowsInSection
但在那之后我有 println()
并且它给了我完整的联系人姓名列表。我不明白为什么这一切都会发生,请帮助我。回顾一下:在 Xcode 中,我有完整的联系人列表,但在设备上我只有最后一个和 5 次。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
let allPeople = ABAddressBookCopyArrayOfAllPeople(AddressBookForVC).takeRetainedValue() as NSArray
for person: ABRecordRef in allPeople{
let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as! String? ?? ""
let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as! String? ?? ""
let phones: ABMultiValueRef = ABRecordCopyValue(person,kABPersonPhoneProperty).takeRetainedValue()
let countOfPhones = ABMultiValueGetCount(phones)
for index in 0..<countOfPhones{
let phone = ABMultiValueCopyValueAtIndex(phones, index).takeRetainedValue() as! String
// println("\(firstName) \(lastName) \(phone)")
cell.textLabel?.text = firstName
println(firstName)
}
}
// cell.textLabel?.text = swiftBlogs[row]
return cell
}
cellForRowAtIndexPath
对 table 中的每一行调用一次。每次调用 cellForRowAtIndexPath
时,您每次都在提取和遍历所有联系人的数组。
copying/looping 所有联系人的代码在 cellForRowAtIndexPath
中都没有意义。通常你会更早地提取所有这些信息(例如在 viewDidLoad
中),然后填充一个数组一次。然后 cellForRowAtIndexPath
将简单地在该数组中查找数据,而不是每次都返回地址簿并重新提取所有联系人。