0

我正在尝试在我的自定义联系人簿中实现搜索栏。我为搜索数据创建了 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]]];
        }*/

    }
}
4

0 回答 0