我正在尝试从 iOS 上的通讯录中检索联系人。
在我的 NSLOG 中似乎一切正常,但是当我将所有联系人放到 tableview(标签等)时,它只显示白色单元格和附件但 3 次(我只有 3 个联系人,并且在这种情况下工作得很好)
@interface ViewController ()
@end
@implementation ViewController
@synthesize searchBar,contactsTableView, retrievedContacts;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"All Contacts";
self.tableData = [[NSMutableArray alloc] init];
retrievedContacts = [[NSMutableArray alloc] init];
self.contactsTableView.dataSource = self;
self.contactsTableView.delegate = self;
[self getPersons];
[self.contactsTableView reloadData];
}
-(void)getPersons {
// [retrievedContacts removeAllObjects];
int i;
ABAddressBookRef contactBook = ABAddressBookCreate();
NSMutableArray *allData = (__bridge_transfer NSMutableArray *)(ABAddressBookCopyArrayOfAllPeople(contactBook));
CFIndex contactNum = CFArrayGetCount((__bridge CFArrayRef)(allData));
for (i = 0; i < contactNum; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex((__bridge CFMutableArrayRef)(allData), i);
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
phonesNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
[retrievedContacts addObject:(__bridge id)(firstName)];
[retrievedContacts addObject:(__bridge id)(lastName)];
[retrievedContacts addObject:(__bridge id)(phonesNum)];
NSLog(@"First name %@", firstName);
NSLog(@"Last Name %@", lastName);
NSLog(@"Phone %@", phonesNum);
}
NSLog(@"Count Contacts %li", contactNum);
//self.tableData = retrievedContacts;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [retrievedContacts count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Cell";
abCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:cellID];
}
cell.firstNameLabel.text = [retrievedContacts objectAtIndex:indexPath.row];
cell.lastNameLabel.text = [retrievedContacts objectAtIndex:indexPath.row];
return cell;
}
这里是 NSLOG 输出:
2013-05-28 15:50:39.260 GTCallBack[39242:c07] First name: Anton
2013-05-28 15:50:39.261 GTCallBack[39242:c07] Last Name: SAnton
2013-05-28 15:50:39.262 GTCallBack[39242:c07] Phone numbers: ABMultiValueRef 0x797e6e0 with 2 value(s)
0: _$!<Mobile>!$_ (0x797ee30) - +972 (58) 123 4567 (0x797ee50)
1: iPhone (0x7976cc0) - +972 (58) 123 4567 (0x797ee10)
2013-05-28 15:50:39.262 GTCallBack[39242:c07] Contact Image: Anton
2013-05-28 15:50:39.263 GTCallBack[39242:c07] First name: Anton
2013-05-28 15:50:39.263 GTCallBack[39242:c07] Last Name: Anton
2013-05-28 15:50:39.264 GTCallBack[39242:c07] Phone numbers: ABMultiValueRef 0x6d8c560 with 1 value(s)
0: _$!<Mobile>!$_ (0x6d8c940) - (058) 123 4567 (0x6d8c960)
2013-05-28 15:50:39.268 GTCallBack[39242:c07] Contact Image: Anton
2013-05-28 15:50:39.269 GTCallBack[39242:c07] First name: Shalom
2013-05-28 15:50:39.270 GTCallBack[39242:c07] Last Name: Shalom
2013-05-28 15:50:39.270 GTCallBack[39242:c07] Phone numbers: ABMultiValueRef 0x7c689d0 with 1 value(s)
0: _$!<Mobile>!$_ (0x7c604c0) - (058) 123 4567 (0x7c6bff0)
2013-05-28 15:50:39.271 GTCallBack[39242:c07] Contact Image: Shalom
2013-05-28 15:50:39.301 GTCallBack[39242:c07] Count Contacts 3
也许还有另一种方法可以用联系人数据填充表格?
谢谢