【问题标题】:SearchDisplayController weird behaviour on UITableView HeaderViewUITableView HeaderView 上的 SearchDisplayController 奇怪行为
【发布时间】:2016-05-21 08:38:38
【问题描述】:

我一直想知道如何针对这种情况实施最佳实践。

我有一个 UITableViewController,并且想以编程方式添加 searchBar,所以我在 viewForHeaderInSection 上这样做了

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
searchBar.placeholder = @"Search";
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchBar.delegate=self;
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self; //This one keep messing with the layout
[view setBackgroundColor:[UIColor blackColor]];
[view addSubview:searchBar];
return view;
}

如下图所示,在编辑时导致我的 searchBar 位置和行为怪异的注释行。

第一响应者使状态栏重叠,虽然我已经添加了我的 viewDidLoad

self.edgesForExtendedLayout = UIRectEdgeNone;

在编辑它时会导致:搜索栏加倍

如果我删除了 searchDisplayController.searchResultsDelegate = self;所有的视图看起来都很完美,但我需要这个委托来触发 searchResultTableView 上的 didSelectedRow。这怎么可能?这种情况的最佳方法是什么。谢谢!

更新

由于我在这里和那里搜索,并且在给出的答案的帮助下,我可以得出结论,如果你想在 UITableView 上修复 SearchBar,只需在 UIViewController 上创建 tableview,放置 searchBar,放置一些约束,然后瞧!

【问题讨论】:

  • 我提供了一个答案,但我刚刚意识到您是在使用 Objective-C 而不是 Swift 进行编码。我很抱歉。不过,转换为objective-c应该很容易。这是完全相同的过程,只是语法略有不同。
  • 我在我的答案中添加了一个链接,它将向您展示如何在 Objective-c 中执行此操作。祝你好运!

标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller


【解决方案1】:

不要在 viewForHeaderInSection 中创建你的 searchController,而是像这样为你的 viewController 创建一个 searchController 变量:

let searchController = UISearchController(searchResultsController: nil)

然后使用这个函数进行配置,并在viewDidLoad中调用这个函数:

func configureSearchController() {
    searchController.searchBar.delegate = self
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.barTintColor = UIColor.whiteColor()
    searchController.searchBar.tintColor = UIColor.blackColor()
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar
}

Objective-C

这里有一个很好的链接,说明如何在 Objective-c 中实现这一点: How to implement UISearchController with objective c

【讨论】:

  • 嗨,谢谢你的回答,确实我不应该把它们放在 viewForHeaderInSection 上,因为我试过像你上面说的那样把它们放在上面,而且很完美,但我希望搜索栏像浮动一样部分,使其不会向上滚动。 tableView.tableHeaderView 似乎不能给我想要的。您对此有什么建议吗?
  • 啊,所以你想让它浮动。最好的办法是使用 viewController 而不是 tableViewController。在 viewController 中放置一个 tableView,在 tableView 顶部放置一个 searchBar。我可以花时间把它写出来,但是通过快速的谷歌搜索,我发现了这个堆栈溢出答案,它应该解释如何完成你想要完成的事情:stackoverflow.com/questions/24796274/…
  • 是的,我想没有其他选择。谢啦!我会接受你的回答。
  • 不客气!实施起来并不难,应该会得到你想要的结果。只是不要忘记在将 tableView 委托和数据源添加到 viewController 时连接它!
  • 是的,现在一切都完美了!谢谢!
猜你喜欢
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多