2017-06-22 92 views
0

這是我創建UIsearchbar的代碼。 我想這個搜索欄上工作的UITableView如何在UIsearchbar上創建用於搜索的func過濾器

//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!) 
    } } 

回答

0

步驟如何開始使用它來創建功能過濾器。

  • 創建兩個數據源。
  • 一個保持所有數據(dataSourceFull)和其他保留過濾數據(dataSourceFiltered)。
  • 論的UISearchBar的文本的變化,用NSpredicate過濾原始數據源(dataSourceFull)並保持過濾後的數據在 (dataSourceFiltered)。
  • 使用tableview綁定過濾的數據(dataSourceFiltered)並重新加載它。
  • 如果搜索文本清除,請清空過濾的數據源。
+0

你可以展示一下這個例子 –