1

为什么我在搜索栏中插入一些单词后会显示另一个表格视图?

在搜索框中写入之前

在此处输入图像描述

在搜索框中写入后

在此处输入图像描述

这是我正在使用的代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@", filteredFields[indexPath.row]];
    }
    else
    {
        cell.textLabel.text = [NSString stringWithFormat:@"%@", oriFields[indexPath.row]    ];
    }


    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    filteredFields = (NSMutableArray *)[oriFields filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}
4

2 回答 2

1

搜索显示控制器管理搜索栏的显示,以及显示搜索结果的表格视图。

您使用搜索栏和负责管理要搜索的数据的视图控制器初始化搜索显示控制器。当用户开始搜索时,搜索显示控制器将搜索界面叠加在原始视图控制器的视图上,并在其表格视图中显示搜索结果。

苹果引用

于 2014-03-05T07:54:09.727 回答
0

you probably use a NSMutableArray as your data source... And you don't clean it when you type your search, so you have twice the values in it... Isn't it ?

于 2014-03-05T07:55:35.937 回答