2013-04-27 46 views
0

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

表视图保持不变。它现在正在工作。

+0

电话号码(个人,工作...)和电子邮件地址(个人,工作...)是数组,而不是字符串。 – 2013-04-27 06:49:15

+0

是的,他们是多值。但是我只为每个联系人提取一个电话号码并将其分配给一个字符串。在控制台上打印字符串工作正常。 – 2013-04-27 07:02:27

回答

1

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

分配使用 cell = [[self.phoneContacts objectAtIndex:indexPath.row] objectForKey:@"AddressBook"] 是行不通的,除非你定义phoneContacts是的UITableViewCell子细胞。

+0

注释部分仅给出联系人名称,如果[phoneContacts addObject:contact]被执行而不是[phoneContacts addObject:curContact],那么也是如此。我不明白我如何才能使phoneContacts成为UITableViewCell的一个子类...对不起,我是新的.. – 2013-04-27 07:40:08

+1

你应该处理你的数组以获得包含联系人名称和编号的'NSString',通过使用'[NSString stringWithFormat ]'或'[NSString stringByAppendingString]'等,然后将你的cell.textLabel.text设置为该NSString。不要做单元格== xxx ... 您也可以在单元格中显示多个文本框以显示不同的信息,例如1个文本框显示名称,另一个显示编号。 – Ooops 2013-04-27 08:11:20

+0

我希望我能够upvote上面的评论...谢谢一吨! ñ是的,我已经改变cell = xx cell.textlabel.text = xxx,但它的strill不工作,直到我只是附加字符串.. :) – 2013-04-27 09:04:55

相关问题