0

我正在检索联系人姓名和电话号码。从我的应用程序中的地址簿。我在日志中打印它们,它工作正常。但是当我尝试在表格视图上显示它们时,我得到了异常 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];
}

表格视图保持不变,如原始帖子中所示。它现在正在工作。

4

1 回答 1

1

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

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

于 2013-04-27T07:20:52.040 回答