iOS搜索地址簿联系人UISearchBar使用带有多个数组的NSDictionary

时间:2013-05-30 18:25:55

标签: ios search uisearchbar addressbook

我正在尝试在自定义联系人簿中实现搜索栏。我为搜索数据创建了NSDictionary,并在我的字典中添加了4个可变数组,包括  姓名,姓氏,电话类型,电话号码。

当我尝试在NSDictionary中搜索时,我的应用程序在单元格创建时崩溃了。当我只搜索一个数组(从字典valueforkey或直接从数组)时,它正在部分工作,但总是从列表中的第一个联系人获取另一个字符串(例如姓氏)。因为循环中不是所有参数,所以我必须按每个参数进行搜索,以便在我的联系人中获得正确的名字和姓氏。

这是我的代码:

#pragma mark SEARCH_BAR

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    [self.tableSearchBar resignFirstResponder];
}



-(void)searchBar:(UISearchBar *)serachBar textDidChange:(NSString *)searchText {

    [filteredStrings removeAllObjects];



    if (searchText.length == 0) {

        isFiltered = NO;

    } else {

        isFiltered = YES;

//这里的目标是用4个可变数组创建NSDictionary

        NSDictionary *filteredContacts = [NSDictionary dictionaryWithObjectsAndKeys:
                                          contacts.firstNames, @"First Name",
                                          contacts.lastNames, @"Last Name",
                                          contacts.phoneTypes, @"Phone Type",
                                          contacts.phoneNumbers, @"Phone Numbers",
                                          nil];


        //[filteredStrings addObject:filteredContacts];

        for (NSString *str in filteredContacts) { // in this case for (NSString *str in [filteredContacts valueForKey:@"First Name"]) all working partially
            NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (stringRange.location != NSNotFound) {

                filteredStrings = [[NSMutableArray alloc]init];

                [filteredStrings addObject:str];
            }
        }



    }


    [self.contactsTableView reloadData];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (isFiltered) {
        return [filteredStrings count];
    }
    return [contacts.firstNames count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell";

    abCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (!cell) {
       cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }

    if (!isFiltered) {

        cell.firstNameLabel.text = [contacts.firstNames objectAtIndex:indexPath.row];
        cell.lastNameLabel.text = [contacts.lastNames objectAtIndex:indexPath.row];
        cell.contactImage.image = [UIImage imageNamed:@"NOIMG.png"];

    } else {

    cell.firstNameLabel.text = [filteredStrings objectAtIndex:indexPath.row]; // Here my app is crashing when trying to search by all nsdictionary.
    cell.lastNameLabel.text = [filteredStrings objectAtIndex:indexPath.row];
    //cell.contactImage.image = [UIImage imageNamed:@"NOIMG.png"];

    }

    return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.contactsTableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([[segue identifier] isEqualToString:@"NextView"]) {

        //if (!isFiltered) {
        ContactDetailsViewController *nextController = [segue destinationViewController];
        NSIndexPath *indexPath = [[[self contactsTableView] indexPathsForSelectedRows] objectAtIndex:0];
        [nextController setPhoneTypes:[contacts.phoneTypes objectAtIndex:[indexPath row]]];
        [nextController setPhoneNumbers:[contacts.phoneNumbers objectAtIndex:[indexPath row]]];
    //}

        /*else {

            ContactDetailsViewController *nextController = [segue destinationViewController];
            NSIndexPath *indexPath = [[[self contactsTableView] indexPathsForSelectedRows] objectAtIndex:0];
            [nextController setPhoneTypes:[contacts.phoneTypes objectAtIndex:[indexPath row]]];
            [nextController setPhoneNumbers:[contacts.phoneNumbers objectAtIndex:[indexPath row]]];
        }*/

    }
}

0 个答案:

没有答案