简体   繁体   English

如何使用按钮显示“搜索显示控制器”

[英]How to show “Search Display Controller” using a button

I have a weird requirement, or at least something I have never done before... I have a UITableView , the header of that table is a UISearchBar . 我有一个奇怪的要求,或者至少是我以前从未做过的事情...我有一个UITableView ,该表的标题是UISearchBar If you tap on that searchbar the "Search Display Controller" shows up and let you search... But now I need to remove that searchbar and call the "Search Display Controller" from a button. 如果您点击该搜索栏,则会显示“搜索显示控制器”,并让您进行搜索...但是现在我需要删除该搜索栏,然后从按钮中调用“搜索显示控制器”。

The problem is that if I remove the SearchBar when I call (from the button): 问题是,如果我在调用时(从按钮中)删除了SearchBar:

   [self.searchDisplayController setActive:YES animated:YES]

I can't see the UISearchBar on top of the View. 我在视图顶部看不到UISearchBar。

I have no idea how to fix this... I have already tried: 我不知道如何解决此问题...我已经尝试过:

  • self.tableView.tableHeaderView = nil in the viewDidLoad and when touching the button I set the UISearchBar as the table header again... it looks ugly and the animation is not smooth at all. self.tableView.tableHeaderView = nil在viewDidLoad中,当触摸按钮时,我再次将UISearchBar设置为表格标题...它看起来很丑,动画一点也不流畅。
  • hide the searchbar and show it when the button is pressed. 隐藏搜索栏并在按下按钮时显示它。 (i get an empty space in the tableview's header) and the animation is really bad, because the UISearchBar gets first displayed in the header and the it's moved up to the top of the view... (我在tableview的标题中留出了一个空白空间),并且动画确实很糟糕,因为UISearchBar首先显示在标题中,并且它向上移动到视图的顶部...

Any help will be more than welcome!! 任何帮助都将非常受欢迎!! Thanks! 谢谢!

I realise that you have tried that, but thats the way we do it and it works: 我意识到您已经尝试过了,但这就是我们做到的方式,并且有效:

We add the UISearchBar into the view (not the table view). 我们将UISearchBar添加到视图(而不是表视图)中。 That being said you can't do that if you are using a UITableViewController. 话虽这么说,如果您使用的是UITableViewController,则无法执行此操作。 You have to add the tableView to a UIViewController. 您必须将tableView添加到UIViewController中。 Then we link it via an Outlet to the Controller and then set it to hidden in viewDidLoad: 然后,我们通过插座将其链接到控制器,然后将其设置为在viewDidLoad中隐藏:

self.searchBar.hidden = YES;

Then create a selector to show the searchBar again: 然后创建一个选择器以再次显示searchBar:

- (IBAction)search:(id)sender
{
  self.searchBar.hidden = NO;

  [self.searchBar becomeFirstResponder];

  [self.searchDisplayController setActive:YES animated:YES];
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在其他View Controller中使用搜索栏和Search Display Controller显示数据 - How to show data with Search Bar and Search Display Controller in other View Controller 单击搜索栏中的输入按钮后,如何更新视图控制器以显示搜索结果? - how to update the view controller to show the search results after clicked enter button in search bar? 如何使用Swift在视图控制器中隐藏/显示按钮? - How to hide/show button in view controller using Swift? 使用NSFetchedResultsController和Search Display Controller搜索CoreData - Searching CoreData using NSFetchedResultsController and Search Display Controller 单击取消按钮时,搜索显示控制器消失 - Search display controller disappears on multiple clicks on cancel button 搜索显示控制器的问题 - Problems with Search Display Controller 搜索显示控制器搜索栏 - Search Display Controller Search Bar 搜索显示控制器是否可以在搜索文本字段为空时隐藏取消按钮? - Search display controller prevent hiding the cancel button if the search text field is empty? 使用搜索显示控制器时隐藏普通的UITableView - Hide normal UITableView when using Search Display Controller iOS:使用搜索控制器后,重新出现已删除的取消按钮 - iOS: Removed cancel button reappears after using search controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM