Pirlo Lochisomo
Pirlo Lochisomo

Reputation: 183

How to show tableview when user taps searchbar swift

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?

Upvotes: 4

Views: 2234

Answers (1)

paulvs
paulvs

Reputation: 12053

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

Upvotes: 5

Related Questions