3

我正在尝试制作像 iPhone 联系人模块一样的联系人表格视图。我有一个按字母顺序排序的表格视图header sections(ABC-...)。现在我想搜索这个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 this 中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];

    }

此过滤列表填充了正确的搜索结果。

编辑 故事板的屏幕截图

在此处输入图像描述

编辑搜索委托方法

#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;
}
4

1 回答 1

0

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

当我将两个搜索显示控制器放在一个视图控制器中时。只有一个搜索显示有效。好的有这个插座连接。见我的帖子

于 2013-08-31T09:50:26.873 回答