我正在检索联系人姓名和电话号码。从我的应用程序中的地址簿。我在日志中打印它们,它工作正常。但是,当我尝试在表格视图中显示它们时,我收到异常NSInvalidArgumentException。我有视图控制器上的按钮,按下该表视图应该得到与该联系人的名字和他们的编号S填充:显示联系人姓名和号码。从地址簿,抛出异常NSInvalidArgumentException
- (IBAction)syncContacts:(id)sender
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
NSString *contact = (NSString *)CFBridgingRelease(ABRecordCopyCompositeName(ref));
// NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref,kABPersonPhoneProperty);
// if (ABMultiValueGetCount(phoneNumbers) > 0) {
NSString *phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
// }
NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)contact,@"Name",phone,@"phone",nil];
[self.phoneContacts addObject:curContact];
}
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
NSLog(@"%@",self.phoneContacts);
NSLog(@"%i",[self.phoneContacts count]);
}
而且表视图的方法是:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.phoneContacts count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// cell.textLabel.text = [self.phoneContacts objectAtIndex:indexPath.row];
cell = [self.phoneContacts objectAtIndex:indexPath.row];
return cell;
}
什么问题与表视图?当phoneContacts只有名称时,它工作正常。([phoneContacts addObject:contact])。但是现在当我添加字典对象时,它抛出了这个异常。
我做了更改。
cell = [[self.phoneContacts objectAtIndex:indexPath.row] objectForKey:@"AddressBook"];
现在没有例外。但是屏幕上没有显示任何内容。
下面是经过编辑的方法:如在原岗位给出
- (IBAction)syncContacts:(id)sender
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
// NSNumber *contact = (NSNumber *)ABRecordCopyComposite();// (ref));
NSString *contact = (NSString *)CFBridgingRelease(ABRecordCopyCompositeName(ref));
// NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref,kABPersonPhoneProperty);
// if (ABMultiValueGetCount(phoneNumbers) > 0) {
NSString *phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
// }
// NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)contact,@"Name",phone,@"phone",nil];
contact = [contact stringByAppendingString:@" "];
contact = [contact stringByAppendingString:phone];
[self.phoneContacts addObject:contact];
}
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
}
表视图保持不变。它现在正在工作。
电话号码(个人,工作...)和电子邮件地址(个人,工作...)是数组,而不是字符串。 – 2013-04-27 06:49:15
是的,他们是多值。但是我只为每个联系人提取一个电话号码并将其分配给一个字符串。在控制台上打印字符串工作正常。 – 2013-04-27 07:02:27