我希望将联系人添加到表格视图,并且我可以使用以下代码将所有联系人添加到我的表格视图,但我想将所选联系人从电话联系人添加到我的表格视图。 可以任何人请帮我如何更改以下代码.....
-(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;
}
答案 0 :(得分:0)
您没有指定您要在什么条件下迭代联系人..
在将contactsConcct dictionay添加到contactsToBeAdded数组之前使用if()进行检查
if([firstName hasPrefix:@"A"])
{
NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)firstName,@"firstName",lastName,@"lastName",organization,@"organization",nil];
[contactsToBeAdded addObject:curContact];
}
我认为这会有所帮助.. !!