iOS のアドレス帳から連絡先を取得しようとしています。
私のNSLOGではすべて問題ないようですが、すべての連絡先をテーブルビュー(ラベルなど)に配置すると、白いセルとアクセサリのみが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
連絡先データでテーブルを埋める別の方法があるでしょうか?
ありがとう