How to show tableview when user taps searchbar swi

2019-02-23 09:07发布

问题:

I need a tableview to be presented modally after user taps on the search bar, and then be able to have the tableview present things the user searched, like Instagram's search function. There are plenty of tutorials on google about how to make a search feature, but none about how to present a tableview after the user taps on the search bar. How should I do that?

回答1:

To make the search results of a UISearchController appear in a new UITableView of your choice, do this:

let searchController = UISearchController(searchResultsController: myTableViewController)

instead of this:

let searchController = UISearchController(searchResultsController: nil)

Passing a view controller to the constructor allows the UISearchController to display the view controller at appropriate times (i.e. when the search is active). This is what you need to do.

Passing nil means, I'm using my own view controller (and table view).