如何在用户点击搜索栏swift时显​​示tableview

问题描述:

我需要在用户点击搜索栏后以模态方式呈现tableview,然后能够让tableview呈现用户搜索的内容,例如Instagram的搜索功能。谷歌有很多关于如何制作搜索功能的教程,但没有关于如何在用户点击搜索栏后呈现桌面视图的教程。我应该怎么做?

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?

制作 UISearchController $ c $的搜索结果c>出现在您选择的新 UITableView 中,执行以下操作:

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

let searchController = UISearchController(searchResultsController: myTableViewController)

而不是:

let searchController = UISearchController(searchResultsController: nil)

将视图控制器传递给构造函数允许 UISearchController 在适当的时间显示视图控制器(即搜索处于活动状态时)。这就是你需要做的。

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.

传递 nil 意味着,我正在使用我自己的观点控制器(和表视图)