履行

首先,让你的类符合 UISearchResultsUpdating 协议。

class MyTableViewController: UITableViewController, UISearchResultsUpdating {}

添加搜索控制器属性:

class MyTableViewController: UTableViewController, UISearchResultsUpdating {
    let searchController = UISearchController(searchResultsController: nil)
}

添加搜索栏:

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar
}

最后,实现来自 UISearchResultsUpdating 协议的 updateSearchResultsForSearchController 方法:

func updateSearchResultsForSearchController(searchController: UISearchController) {

}