简体   繁体   中英

Need to use UISearchController without UITableViewController like a UISearchDisplayController

So the scenario is that i'm implementing a searchbar on top of a mapView. So my class isnt UITableViewController. Previously it was using searchDisplayController, but since that is depreciated, i want to avoid using it. the search display controller was using UITableViewDataSource to manipulate table rows and such.

Can i still make rows appear on top while i search? the tutorials i can find for UISearchController are using 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. I have attached the screenshot for the storyBoard and here is the code to handle the hide and unhiding of 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. Don't forget to set the searchBar delegate to self. Hope this helps

By using UISearchBar delegates you can achieve your task.

Step1: Add UISearchBar to your view from show object library.

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

Step2: Add UISearchBar outlet to your viewcontroller class.

@IBOutlet weak var searchbar: UISearchBar!

Step3: Conform UISearchBar delegate to your viewcontroller class.

    `searchbar.delegate = self`

Step4: Use all UISearchbar delegate methods you will finish your scenario.

@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 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM