简体   繁体   English

需要像UISearchDisplayController一样使用没有UITableViewController的UISearchController

[英]Need to use UISearchController without UITableViewController like a UISearchDisplayController

So the scenario is that i'm implementing a searchbar on top of a mapView. 因此,情况是我要在mapView顶部实现搜索栏。 So my class isnt UITableViewController. 所以我的课不是UITableViewController。 Previously it was using searchDisplayController, but since that is depreciated, i want to avoid using it. 以前它使用的是searchDisplayController,但由于已贬值,我想避免使用它。 the search display controller was using UITableViewDataSource to manipulate table rows and such. 搜索显示控制器正在使用UITableViewDataSource来操纵表行等。

Can i still make rows appear on top while i search? 搜索时是否仍可以使行显示在顶部? the tutorials i can find for UISearchController are using UITableView. 我可以为UISearchController找到的教程都使用UITableView。 I want table rows just to display me my search result and disappear when i select a row and the search bar should resigns as well. 我希望表格行仅显示我的搜索结果,并在我选择一行时消失,并且搜索栏也应辞职。

Can anyone guide me in the right direction? 谁能指导我正确的方向?

EDIT: i am currently using a searchbar made from the storyboard 编辑:我目前正在使用由情节提要制成的搜索栏

If you need a design like a list, best option is a tableview. 如果您需要像列表这样的设计,最好的选择是表格视图。 You can add a tableview below your search bar, hide it initially and then display it once user selects the searchBar for searching. 您可以在搜索栏下方添加一个表格视图,先将其隐藏,然后在用户选择要搜索的searchBar后将其显示。 I have attached the screenshot for the storyBoard and here is the code to handle the hide and unhiding of tableview 我已附加了storyBoard的屏幕截图,这是用于处理tableview的隐藏和取消隐藏的代码 在此处输入图片说明

Here is the code: 这是代码:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    tableViewForSearch.isHidden = false

}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    tableViewForSearch.isHidden = true
}

These are searchBar delegates, they are quite descriptive. 这些是searchBar委托,它们具有很强的描述性。 Don't forget to set the searchBar delegate to self. 不要忘记将searchBar委托设置为self。 Hope this helps 希望这可以帮助

By using UISearchBar delegates you can achieve your task. 通过使用UISearchBar委托,您可以完成任务。

Step1: Add UISearchBar to your view from show object library. 步骤1:从显示对象库向您的视图添加UISearchBar。

/Users/rajesh/Desktop/Screen Shot 2017-10-05 at 11.17.14 AM.png / Users / rajesh / Desktop / Screen Shot 2017-10-05 at 11.17.14 AM.png

Step2: Add UISearchBar outlet to your viewcontroller class. 步骤2:将UISearchBar插座添加到您的viewcontroller类。

@IBOutlet weak var searchbar: UISearchBar!

Step3: Conform UISearchBar delegate to your viewcontroller class. 第3步:使UISearchBar委托符合您的viewcontroller类。

    `searchbar.delegate = self`

Step4: Use all UISearchbar delegate methods you will finish your scenario. 步骤4:使用所有UISearchbar委托方法,您将完成方案。

@available(iOS 2.0, *) optional public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool // return NO to not become first responder @available(iOS 2.0, *) optional public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool //返回NO,不成为第一响应者

@available(iOS 2.0, *)
optional public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) // called when text starts editing

@available(iOS 2.0, *)
optional public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool // return NO to not resign first responder

@available(iOS 2.0, *)
optional public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) // called when text ends editing

@available(iOS 2.0, *)
optional public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) // called when text changes (including clear)

@available(iOS 3.0, *)
optional public func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool // called before text changes


@available(iOS 2.0, *)
optional public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) // called when keyboard search button pressed

@available(iOS 2.0, *)
optional public func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) // called when bookmark button pressed

@available(iOS 2.0, *)
optional public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) // called when cancel button pressed

@available(iOS 3.2, *)
optional public func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) // called when search results button pressed


@available(iOS 3.0, *)
optional public func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int)

暂无
暂无

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

相关问题 为没有UISearchBar的UITableViewController设置UISearchDisplayController - Set up a UISearchDisplayController for a UITableViewController without a UISearchBar 有谁知道如何在不使用StoryBoards的情况下使用UITableViewController在Xamarin.iOs中实现UISearchController? - Does anyone know how to use UITableViewController to implement UISearchController in Xamarin.iOs without using StoryBoards? 实现UISearchController和UISearchDisplayController - Implement both UISearchController and UISearchDisplayController 使用没有故事板的UISearchController - Use UISearchController without storyboard UISearchController不在UITableViewController中搜索 - UISearchController not searching in UITableViewController UITableViewController与UISearchDisplayController多个选择同步 - UITableViewController with UISearchDisplayController multiple selection sync 使用UITableViewController,UISearchDisplayController和'automaticallyAdjustsScrollViewInsets'隐藏UISearchBar - Hide UISearchBar with UITableViewController, UISearchDisplayController and 'automaticallyAdjustsScrollViewInsets' 如何在没有UITableViewController的情况下使用UITableView - how to use a UITableView without a UITableViewController 在UITableViewController中使用UISearchDisplayController时断言失败 - Assertion failure when using UISearchDisplayController in UITableViewController iOS - 在带有静态单元格的UITableViewController上使用UISearchDisplayController - iOS - Using a UISearchDisplayController on a UITableViewController with static cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM