0
votes

i want add contacts to a table view and i am able to add all contacts to my table view by using the following code but i want to add selected contacts from phone contacts to my table view . can any one please help me how to change the following code .....

-(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) firstName=@"";
        if(!lastName) lastName=@"";
        if(!organization) organization=@"";
        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;
}
1

1 Answers

0
votes

you didn't specify on what condition you are going to iterate contacts..

just check using if () before adding curContact dictionay to contactsToBeAdded array

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

{


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

[contactsToBeAdded addObject:curContact];

}

i think this will help..!!