ios - 如何在 UITableView 中显示/隐藏 UISearchController searchBar?

标签 ios objective-c uitableview uisearchbar uisearchcontroller

在我的应用程序中,我有以下设置:

  • UIViewController
  • 作为 subview 的 UITableView
  • tableView 有一个 UISearchController 和一个搜索栏是一个表头 View

    _tableView.tableHeaderView = _searchController.searchBar;

我想实现的是:

  • 出现此屏幕时,搜索栏不存在
  • 点击某个按钮(例如导航 Controller 右键)出现搜索栏(可能从顶部动画或其他)
  • 再次点击该按钮搜索栏隐藏

当搜索栏没有显示表格时,看起来好像那个栏从来没有出现过(没有空的标题单元格或类似的东西)

非常感谢任何形式的帮助!

最佳答案

这是我自己想出来的,也许不是最佳解决方案,但如果有人有更好的想法,我会很乐意接受它作为答案。

-(void)searchButtonDidTap:(UIButton*)aButton
{    
     if (!_searchController){

     _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];

    _searchController.searchResultsUpdater = self;
    [_searchController.searchBar sizeToFit];
    _tableView.tableHeaderView = _searchController.searchBar;

    _searchController.delegate = self;
    _searchController.dimsBackgroundDuringPresentation = NO;

    self.definesPresentationContext = YES;
    _searchController.active = NO;

    _searchController.searchResultsUpdater = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    _tableView.tableHeaderView = _searchController.searchBar;

    _searchController.searchBar.delegate = self;
    }
    else
    {
    _searchController.active = NO;
    [_searchController removeFromParentViewController];
    _searchController = nil;
    _tableView.tableHeaderView = nil;
    }

    [_tableView reloadData];
}

关于ios - 如何在 UITableView 中显示/隐藏 UISearchController searchBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324864/

相关文章:

ios - 无法禁用 UITableView 分组中部分的页脚

ios - 如何使表格 View 在快速重新加载数据后显示特定行?

iOS:registerForRemoteNoficationTypes,接下来会发生什么?

iphone - 后备语言似乎不起作用(iOS/iPhone)

swift - 如何在自定义表格单元格中正确对齐以编程方式创建的开关

objective-c - 如何使用特定语言的本地化 UIImage?

iphone - 加载桌面版网站

ios - Facebook Open Graph Story 仅对我可见

ios - TableView IndexPath.row 不可分配

ios - 设置 SelectionIndicatorImage 对于 iphone 6 和 iPhone 6+ 的尺寸错误