I have an application in which I have a textfiled.Just beneath the text filed I am adding a tableview. When I starting typing a letter in textfield it searches for address book and display the name along with the email address of that person on tableview. But my problem is it is not displaying emailaddress along with name on my tableview.
This is my code
-(void)textFieldDidChange:(UITextField *)txtFld {
[self fetchAddressBook];
NSString *dictionaryKey = contact.name;
NSString *predicateString = contact.email;
//---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", contact.email];
listFiles = [NSMutableArray arrayWithArray:[self.namearray
filteredArrayUsingPredicate:predicate]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [listFiles sortedArrayUsingDescriptors:sortDescriptors];
if ([sortedArray count]>0)
{
tblView.hidden=FALSE;
txtSendAmount.hidden=TRUE;
txtSendMessage.hidden=TRUE;
[tblView reloadData];
} else if ([sortedArray count]==0) {
tblView.hidden=TRUE;
txtSendAmount.hidden=FALSE;
txtSendMessage.hidden=FALSE; }
}
Initially I am fetching contacts from address book and then using predicate on my contacts array.
This is the code to fetch from address book
-(void)fetchAddressBook
{
CFErrorRef error = nil;
ABAddressBookRef allPeople = ABAddressBookCreateWithOptions(NULL,&error);
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
CFIndex numberOfContacts = ABAddressBookGetPersonCount(allPeople);
NSMutableArray *testarray = [[NSMutableArray alloc] init];
NSMutableDictionary *addressdict = [[NSMutableDictionary alloc] init];
for(int i = 0; i < numberOfContacts; i++){
name = @"";
NSString* phone = @"";
email = @"";
contact = [[MContact alloc] init];
ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);
ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);
ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);
NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
if (fnameProperty != nil) {
contact.name = [NSString stringWithFormat:@"%@", fnameProperty];
}
if (lnameProperty != nil) {
contact.name = [contact.name stringByAppendingString:[NSString stringWithFormat:@" %@", lnameProperty]];
}
if ([phoneArray count] > 0) {
if ([phoneArray count] > 1) {
for (int i = 0; i < [phoneArray count]; i++) {
phone = [phone stringByAppendingString:[NSString stringWithFormat:@"%@\n", [phoneArray objectAtIndex:i]]];
}
}else {
phone = [NSString stringWithFormat:@"%@", [phoneArray objectAtIndex:0]];
}
}
if ([emailArray count] > 0) {
if ([emailArray count] > 1) {
for (int i = 0; i < [emailArray count]; i++) {
contact.email = [contact.email stringByAppendingString:[NSString stringWithFormat:@"%@\n", [emailArray objectAtIndex:i]]];
}
}else {
contact.email = [NSString stringWithFormat:@"%@", [emailArray objectAtIndex:0]];
}
}
[addressdict setObject:contact.email forKey:contact.name];
NSLog(@"%@",addressdict);
NSLog(@"NAME : %@",name);
NSLog(@"PHONE: %@",phone);
NSLog(@"EMAIL: %@",email);
[self.emailnamearray addObject:contact];
self.namearray = [emailnamearray copy];
NSLog(@"namearray: %@",namearray);
}
}
This is my cellforRowAtIndexPath method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:@"eventCell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"eventCell"];
}
MContact *addressdict = [listFiles objectAtIndex:indexPath.row];
cell.textLabel.text=addressdict.name;
cell.detailTextLabel.text=addressdict.email;
return cell;
}