Swift3 如何在UIsearchbar上创建用于搜索的func筛选器

Swift3 如何在UIsearchbar上创建用于搜索的func筛选器,swift3,uisearchbar,Swift3,Uisearchbar,这是我创建UIsearchbar的代码。 我想通过此搜索栏在UItableview上创建函数过滤器 //Make sure to import UIKit import Foundation import UIKit class ViewController: UIViewController, UISearchBarDelegate { var searchController = UISearchController() override func viewD

这是我创建UIsearchbar的代码。 我想通过此搜索栏在UItableview上创建函数过滤器

 //Make sure to import UIKit import Foundation import UIKit

 class ViewController: UIViewController, UISearchBarDelegate {

      var searchController = UISearchController()

     override func viewDidLoad() {
          //Setup search bar
          searchController = UISearchController(searchResultsController: nil)
          searchController.dimsBackgroundDuringPresentation = false
          definesPresentationContext = true
          //Set delegate
          searchController.searchResultsUpdater = self
          //Add to top of table view
          tableView.tableHeaderView = searchController.searchBar
     } } extension ViewController: UISearchResultsUpdating {
     func updateSearchResults(for searchController: UISearchController) {
          print(searchController.searchBar.text!)
     } }

关于如何开始的步骤

  • 创建两个数据源
  • 一个保持所有数据(dataSourceFull),另一个保持过滤数据(dataSourceFiltered)
  • 在UISearchBar的文本更改中,使用NSpredicate过滤原始数据源(dataSourceFull),并将过滤后的数据保留在 (数据源过滤)
  • 将筛选数据(dataSourceFiltered)与tableview绑定并重新加载
  • 在这种情况下,清除搜索文本后,清空过滤后的数据源