SearchDisplay控制器未显示我的搜索结果

时间:2013-02-25 10:53:59

标签: iphone ios objective-c uitableview uisearchdisplaycontroller

我正在尝试制作类似iPhone联系人模块的联系人tableview。我有一个tableview按字母顺序排序header sections(A-B-C -...)。现在我想搜索这个tableview。在我搜索之后,我应该只有一个部分包含所有搜索结果。

我遇到的问题是它一直显示"No results"。但当我查看包含搜索结果的数组时,它包含正确的值。

现在代码。

我的CellForRowAtIndexPath

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (tableView == self.searchDisplayController.searchResultsTableView){
        NSLog(@"search tableview");

        Contact *contact = [self.filteredListContent objectAtIndex:indexPath.row];

        NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
        NSLog(@"CellForRowAtIndexPath contact text is %@",text);
        cell.textLabel.text = text;


        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];


    }else{
        NSLog(@"standaard tableview");
    NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]];

    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@",alphabet];
    NSArray *contacts = [listContent filteredArrayUsingPredicate:predicate];
    Contact *contact = [contacts objectAtIndex:indexPath.row];

    NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
    cell.textLabel.text = text;


       [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }

    return cell;

}

这是问题所在。当我在IF中self.searchDisplayController.isActive并查看此日志NSLog(@"CellForRowAtIndexPath contact text is %@",text)时,它会给我正确的上下文名称。但是,当我改变if为此。 tableView == self.searchDisplayController.searchResultsTableView然后它总是进入ELSE部分。

我的filterContentForSearchText

   - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
    {
        NSLog(@"filterContentForSearchText called");
        /*
         Update the filtered array based on the search text and scope.
         */

        [self.filteredListContent removeAllObjects]; // First clear the filtered array.

        /*
         Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
         */
        for (Contact *contact in listContent)
        {
            NSString *searchString = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
            NSRange range = [searchString rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                [self.filteredListContent addObject:contact];
                [self.searchDisplayController.searchResultsTableView reloadData];
            }
        }
        NSLog(@"filterd list: %@",[self.filteredListContent valueForKey:@"name"]);

        [self.tableView reloadData];

    }

此过滤后的列表中填写了正确的搜索结果。

修改 故事板的屏幕截图

enter image description here

编辑搜索委托方法

#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    // Tells the table data source to reload when scope bar selection changes
     [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

1 个答案:

答案 0 :(得分:0)

尝试创建与联系人表视图控制器的新引用插座连接。

当我在一个视图控制器中放置两个搜索显示控制器时。只有一个搜索显示有效。好的一个有Outlet连接。查看我的post