我有一个表视图,里面有地址簿项(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
}发布于 2015-04-18 13:40:06
对表中的每一行都调用一次cellForRowAtIndexPath。每次调用cellForRowAtIndexPath时,每次都要提取和迭代所有联系人的数组。
在cellForRowAtIndexPath中复制/循环所有联系人的代码没有意义。通常,您会提前提取所有这些信息(例如,在viewDidLoad中),并填充一次数组。然后,cellForRowAtIndexPath将简单地在该数组中查找数据,而不是每次返回地址簿并重新提取所有联系人。
https://stackoverflow.com/questions/29718067
复制相似问题