ios - 显示联系人姓名和电话号码。从地址簿中,抛出异常 NSInvalidArgumentException

标签 ios objective-c xcode ios6 xcode4.5

我正在检索联系人姓名和电话号码。从我的应用程序中的地址簿。我将它们打印在日志中并且工作正常。但是当我尝试在 TableView 上显示它们时,我收到异常 NSInvalidArgumentException。我在 View Controller 上有一个按钮,按下该按钮 TableView 应该填充联系人姓名及其号码:

- (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]);




}

表格 View 方法是:

-(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;


}

TableView 有什么问题?当phoneContacts只有名字时,它工作正常。([phoneContacts addObject:contact])。但是现在当我添加字典对象时,它抛出了这个异常。

我做出了改变。

 cell = [[self.phoneContacts objectAtIndex:indexPath.row] objectForKey:@"AddressBook"];

异常(exception)现在没有出现。但屏幕上没有显示任何内容。

这是编辑后的方法:

- (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];
}

表格 View 保持原始帖子中给出的不变。现在正在运行。

最佳答案

注释行 cell.textLabel.text = [self.phoneContacts objectAtIndex:indexPath.row]; 不起作用?您可以设置断点并检查单元格的文本标签是否已分配文本吗?

使用分配单元格 cell = [[self.phoneContacts objectAtIndex:indexPath.row] objectForKey:@"AddressBook"] 除非您将phoneContacts定义为UITableViewCell的子类,否则不起作用。

关于ios - 显示联系人姓名和电话号码。从地址簿中,抛出异常 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16249037/

相关文章:

objective-c - 如何从 UIView 导航到 UIViewController

ios - HealthKit 权限模式 - 对开始/结束外观转换的不平衡调用

ios - 如何在 Xcode 中创建带有主细节 View 的自定义原型(prototype)单元?

ios - NSInvalidArgumentException',原因 : '-[UITableView setSeparatorInset:]: unrecognized selector sent to instance

javascript - 在 JSConext 的方法中传递 block

ios - geocodePostalAddress - 从邮政编码获取地址

iphone - 设置转换 :CGAffineTransformMakeScale - Alternatives when trying to scale an UIView

swift - 相机 View 作为模糊背景?

iphone - Cocos2d,如何用计时器改变 Sprite 图像并重新重复这个过程?

iphone - popToRootViewController 以及如何在此处更新信息