为什么开始搜索时我的搜索表视图没有出现?

时间:2012-03-19 03:51:18

标签: iphone ios ios5 uisearchbar uisearchdisplaycontroller

我已将UISearchDisplayController与搜索栏连接到我的应用程序中。我已将其初始化为:

self.filteredObjectArray = [NSMutableArray arrayWithCapacity:[self.objectArray count]];

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
searchBar.delegate = self;
[self.view addSubview:searchBar];

UISearchDisplayController *searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplay.delegate = self;
searchDisplay.searchResultsTableView.delegate = self;
searchDisplay.searchResultsDataSource = self;
searchDisplay.searchResultsDelegate = self;

这几乎就是我在其他例子中看到的方式。我还实现了适当的委托方法,重要的filterContentForSearchText和shouldReload *方法如下所示:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{   
    [self.filteredObjectArray removeAllObjects]; // First clear the filtered array.

    for (XRay *xray in objectArray) {
        NSString *combinedLabel = [self combinedLabel:xray];
        if ([combinedLabel rangeOfString:searchText options:(NSCaseInsensitiveSearch)].location != NSNotFound) {
            [self.filteredObjectArray addObject:xray];
        }
    }
}

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

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
     [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
 [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    return YES;
}

但是,当我将搜索文本框设为焦点或输入任何字符时,我的搜索功能无效。您还会注意到灰色视图不会出现在我的常规表视图中。

Screenshot

修改 的cellForRowAtIndexPath

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

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

    XRay *xray = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        xray = [self.filteredObjectArray objectAtIndex:indexPath.row];

    } else {
        xray = [self.objectArray objectAtIndex:indexPath.row];
    }

    cell.textLabel.text = [self combinedLabel:xray];

    return cell;
}

2 个答案:

答案 0 :(得分:2)

我弄清楚出了什么问题,老实说,我有点惊讶。一旦我在SearchTableViewController上设置了一个属性: @property(非原子,强)UISearchDisplayController * searchDisplay;

并更新了我的代码以使用那个,一切都在那里工作。这是我使用新属性的初始化代码。

- (void)viewDidLoad
{
    self.filteredObjectArray = [NSMutableArray arrayWithCapacity:[self.objectArray count]];

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
    searchBar.delegate = self;
    self.tableView.tableHeaderView = searchBar;

    self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

    self.searchDisplay.delegate = self;
    self.searchDisplay.searchResultsDataSource = self;
    self.searchDisplay.searchResultsDelegate = self;
}

答案 1 :(得分:0)

Try with below code it will help you

 self.filteredObjectArray = [NSMutableArray arrayWithCapacity:[self.objectArray count]];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
    searchBar.delegate = self;
    //[self.view addSubview:searchBar];
    self.theTableView.tableHeaderView = searchBar;//theTableView your tableview object

    UISearchDisplayController *searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    self.searchController = searchDisplay;
    [searchBar release];
    searchDisplay.delegate = self;
    searchDisplay.searchResultsDataSource = self;
    searchDisplay.searchResultsDelegate = self;