履行

首先,讓你的類符合 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) {

}