2011-02-08 250 views
0

我想添加联系人到表格视图,我可以通过使用下面的代码将所有联系人添加到我的表格视图,但我想从手机联系人添加选定的联系人到我的表格查看。 任何一个可以请帮我如何更改下面的代码.....从iphone联系人添加联系人到我的表格视图

-(void)loadTableSource{ 
    contactsToBeAdded=[[NSMutableArray alloc] init]; 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); 
    //CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook,CFSTR("Rajesh")); 
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); 
    for(int i=0;i<nPeople;i++){ 
     ABRecordRef person=CFArrayGetValueAtIndex(people, i); 
     NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
     NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
     NSString *organization=(NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty); 
     if(!firstName) [email protected]""; 
     if(!lastName) [email protected]""; 
     if(!organization) [email protected]""; 
     NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)firstName,@"firstName",lastName,@"lastName",organization,@"organization",nil]; 
     [contactsToBeAdded addObject:curContact]; 
    } 
    CFRelease(people); 
    CFRelease(addressBook); 
    [self setTableSource:contactsToBeAdded]; 
    [contactsToBeAdded release]; 
} 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdent=[NSString stringWithFormat:@"c%i",indexPath.row]; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdent]; 
    if(cell==nil){ 
     cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent]; 
     NSDictionary *dict=[tableSource objectAtIndex:indexPath.row]; 
     UILabel *lab=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 310, 40)]; 
     NSLog(@"lable:%@",tableSource); 
     [lab setNumberOfLines:2]; 
     [lab setText:[[NSString stringWithFormat:@"%@ %@\n%@",(NSString *)[dict objectForKey:@"firstName"],[dict objectForKey:@"last_name"],[dict objectForKey:@"organization"]] stringByReplacingOccurrencesOfString:@"(null)" withString:@""]]; 
     [cell.contentView addSubview:lab]; 
     [lab release]; 

    } 
    return cell; 
} 

回答

0

你不上你要重复接触什么情况下指定..

只是检查使用if( )加入curContact百科到contactsToBeAdded阵列

if([firstName hasPrefix:@"A"]) 

{ 


NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)firstName,@"firstName",lastName,@"lastName",organization,@"organization",nil]; 

[contactsToBeAdded addObject:curContact]; 

} 

之前,我认为这将有助于.. !!

+0

谢谢,但我想添加特定的联系方式,如果我点击联系人(姓名)在手机联系人只有该联系人将被添加到我的表格单元格。 – user564963 2011-02-08 11:21:53