未在UITableView中加载的联系人

时间:2015-07-28 08:48:40

标签: ios uitableview

在我的iOS应用程序中,我有一个显示UITableView的ViewController。一切(委托,数据源......)已正确设置。我想做一个常见的事情:用手机中的联系人填充TableView。

但是由于某些原因,TableView始终为空,而我在viewDidLoad中成功获取了联系人。我注意到方法tableView:numberOfRowsInSection:在我获得联系人列表之前被调用,但我不知道该问题是什么。这是我的Controller.m:

@interface Controller () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *contacts;
@property(nonatomic, strong) IBOutlet UITableView *contactsTable;

@end

@implementation Controller

- (void)viewDidLoad {
[super viewDidLoad];
self.contacts = [[NSMutableArray alloc] init];
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
    // if you got here, user had previously denied/revoked permission for your
    // app to access the contacts, and all you can do is handle this gracefully,
    // perhaps telling the user that they have to go to settings to grant access
    // to contacts

    [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    return;
}

CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (!addressBook) {
    NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
    return;
}

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    if (error) {
        NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
    }

    if (granted) {
        // if they gave you permission, then just carry on

        [self listPeopleInAddressBook:addressBook];
    } else {
        // however, if they didn't give you permission, handle it gracefully, for example...

        dispatch_async(dispatch_get_main_queue(), ^{
            // BTW, this is not on the main thread, so dispatch UI updates back to the main queue

            [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        });
    }

    CFRelease(addressBook);
});
}


- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
    ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
        NSLog(@"  phone:%@", phoneNumber);
        GIContact *contact = [[GIContact alloc] initWithFullName:fullname telephone:phoneNumber];
        [self.contacts addObject:contact];
    }

    CFRelease(phoneNumbers);
}
}

//Called before I got the contacts
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
return [self.contacts count]; //This works when I replace it with a number like 2
}
- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create an instance of UITableViewCell, with default appearance
UITableViewCell *cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                       reuseIdentifier:@"ContactTableCell"];
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
   GIContact *contact = self.contacts[indexPath.row];
   cell.textLabel.text = contact.fullname;
   return cell;
}

@end

Contact是一个简单的模型类,有两个NSString(fullname和telephone)

3 个答案:

答案 0 :(得分:3)

成功连接并检索联系人列表后,刷新表格视图。调用numberOfRowsInSection是因为视图立即尝试显示数据,但尚未显示。

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});

答案 1 :(得分:2)

您在tableView reloadData方法调用后获得联系。

- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
    ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
        NSLog(@"  phone:%@", phoneNumber);
        GIContact *contact = [[GIContact alloc] initWithFullName:fullname telephone:phoneNumber];
        [self.contacts addObject:contact];
    }

    CFRelease(phoneNumbers);
}
[self.tableView reloadData];// refresh table Data
}

答案 2 :(得分:1)

方法ABAddressBookRequestAccessWithCompletion使其工作脱离主线程。一个提示,就是尾随Closure作为回调调用,只要方法完成了它的工作。

所以当调用(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section时,它仍然在主线程上运行,或者数据还没有“到达”主线程。

如果要在完成提取地址簿请求后立即填充数据,则应在主线程上重新加载UITableView

- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
    NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
    NSLog(@"  phone:%@", phoneNumber);
    GIContact *contact = [[GIContact alloc] initWithFullName:fullname telephone:phoneNumber];
    [self.contacts addObject:contact];
}

CFRelease(phoneNumbers);
}
// reload the tableview on the main thread here
dispatch_async(dispatch_get_main_queue(), ^{
   [self.tableView reloadData];
});
}