将工具栏添加到UISearchDisplayController的结果中

时间:2012-09-25 06:17:44

标签: ios uisearchbar uisearchdisplaycontroller

我在UISearchDisplayController遇到了困难。在我的场景中,我将UIView推到了一个导航控制器上。在UIView我有一个UITableViewUIToolbar。在UITableView我正在使用UISearchDisplayController

<img></img>

工具栏按钮用于向搜索添加其他过滤器选项。我的问题是我无法弄明白,如何在UISearchDisplayController的结果表视图的底部添加工具栏。

enter image description here

在结果中添加工具栏有什么方法?

2 个答案:

答案 0 :(得分:0)

我终于设法解决了我的问题。

我只是将UISearchBar添加到我的UITableView,并使用UISearchBarDelegate方法复制UISearchDisplayController的行为,而不是使用UISearchDisplayController。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self setSearchText:searchText];
    [self filterCards];
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    [self setScopeIndex:selectedScope];
    [self filterCards];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    // Move searchbar to table view
    [self.chapterSearchBar removeFromSuperview];
    [self.chapterTableView addSubview:[self chapterSearchBar]];

    // Show navigation controller
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    // Hide scope bar an resize
    [searchBar setShowsScopeBar:NO];
    [searchBar sizeToFit];

    // Hide cancel button
    [searchBar setShowsCancelButton:NO animated:YES];

    // Resize table view
    CGRect tableViewRect = [self.chapterTableView frame];    
    tableViewRect.origin.y = 0;
    [self.chapterTableView setFrame:tableViewRect];

    // Hide keyboard
    [searchBar resignFirstResponder];
    [self setSearchText:@""];
    [self filterCards];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    // Move searchbar to controller view
    [self.chapterSearchBar removeFromSuperview];
    [self.view addSubview:[self chapterSearchBar]];

    // Hide navigation controller
    [self.navigationController setNavigationBarHidden:YES animated:YES];

    // Show scope bar an resize
    [searchBar setShowsScopeBar:YES];
    [searchBar sizeToFit];

    // Show cancel button
    [searchBar setShowsCancelButton:YES animated:YES];

    // Resize table view
    CGRect tableViewRect = [self.chapterTableView frame];    
    tableViewRect.origin.y = 44;
    [self.chapterTableView setFrame:tableViewRect];

    return YES;
}

答案 1 :(得分:0)

如果有人好奇如何使用UISearchDisplayController(可能更干净)来解决此问题,只需在搜索处于活动状态时将工具栏的项目设置为视图控制器的toolbarItems

self.navigationController.toolbarHidden = NO;
self.toolbarItems = optionsToolbar.items;

UISearchDisplayController根据toolbarItems保留视图控制器的工具栏,因此这可能已经为您完成了。如果仅在搜索期间使用工具栏,则非常有用。

相关问题