开发者

Phonebook email property return nil while loading in UITableView

开发者 https://www.devze.com 2023-03-27 16:22 出处:网络
In my sample application. I have to fetch all contacts from my iPhone and display each one\'s firstName, lastName, companyName, and email address.

In my sample application. I have to fetch all contacts from my iPhone and display each one's firstName, lastName, companyName, and email address.

I have written code from Apple reference guide, but face some problems while loading contacts in UITableView.

I have retrieved all contacts using the code below:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                                           kCFAllocatorDefault,
                                                           CFArrayGetCount(people),
                                                           people
                                                           );


CFArraySortValues(
                  peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  (void*) ABPersonGetSortOrdering()
                  );

CFRelease(addressBook);
CFRelease(people);
CFRelease(peopleMutable);


ABRecordRef record = [peopleMutable objectAtIndex:i];
ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(record, kABPersonEmailProperty);
NSLog(@"ABMultiValueGetCount %d",ABMultiValueGetCount(emails));
if(ABMultiValueGetCount(emails))
       emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails, 0);

When I use this line开发者_如何转开发 of code in viewdidAppear it works fine. But when i use this same method in the UITableViewDelegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. It didn't return email address.

Has anyone seen a problem like this?


I suppose, that peopleMutable is nil when you access it from tableView: cellForRowAtIndexPath: because you load your data later then tableView reload its data.

Call [tableView reloadData] after populating peopleMutable array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号