在iOS中显示文本字段时,在tableview上显示地址簿中的电子邮件地址和名称

时间:2014-03-24 13:33:04

标签: ios iphone tableview addressbook

我有一个应用程序,我有一个textfiled.Just在文本提交下面我正在添加一个tableview。当我开始在文本字段中键入一个字母时,它会搜索地址簿并在tableview上显示该人的电子邮件地址。但我的问题是它没有在我的tableview上显示emailaddress和名字。

这是我的代码

-(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;    }
     }

最初我从地址簿中获取联系人,然后在我的联系人数组中使用谓词。

这是从地址簿中提取的代码

-(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);
    }
}

这是我的cellforRowAtIndexPath方法

-(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;
}

1 个答案:

答案 0 :(得分:0)

首先检查联系人是否有电子邮件地址。您的问题究竟是什么1)无法获取电子邮件地址2)无法显示已保存的电子邮件地址