Created
April 21, 2018 21:39
-
-
Save marcosgriselli/b4b2b37d610742346140c693a79fdfc9 to your computer and use it in GitHub Desktop.
Basic implementation of a UISearchController reusing the same type of UIViewController as the base and results.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol Searchable { | |
func update(text: String?) | |
} | |
class BaseSearchResultUpdating: NSObject, UISearchResultsUpdating { | |
func updateSearchResults(for searchController: UISearchController) { | |
guard let searchable = searchController.searchResultsController as? Searchable else { | |
fatalError("The searchResultsController is does not conform to Searchable.") | |
} | |
searchable.update(text: searchController.searchBar.text) | |
} | |
} | |
class TableViewController: UITableViewController, Searchable { | |
var elements = ["A" ,"B", "C", "D"] | |
var resultsController: TableViewController? | |
var baseSearchResultUpdating: BaseSearchResultUpdating? | |
init(searchEnabled: Bool) { | |
super.init(style: .plain) | |
if searchEnabled { | |
setSearch() | |
} | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
func setSearch() { | |
resultsController = TableViewController(searchEnabled: false) | |
let searchController = UISearchController(searchResultsController: resultsController) | |
baseSearchResultUpdating = BaseSearchResultUpdating() | |
searchController.searchResultsUpdater = baseSearchResultUpdating | |
searchController.searchBar.placeholder = "Search char" | |
searchController.obscuresBackgroundDuringPresentation = true | |
definesPresentationContext = true | |
searchController.searchBar.sizeToFit() | |
if #available(iOS 11.0, *) { | |
navigationItem.searchController = searchController | |
navigationItem.hidesSearchBarWhenScrolling = true | |
} else { | |
tableView.tableHeaderView = searchController.searchBar | |
} | |
} | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return elements.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
var cell = tableView.dequeueReusableCell(withIdentifier: "cell") | |
if cell == nil { | |
cell = UITableViewCell(style: .default, reuseIdentifier: "cell") | |
} | |
cell?.textLabel?.text = elements[indexPath.row] | |
return cell! | |
} | |
func update(text: String?) { | |
guard let text = text, !text.isEmpty else { return } | |
elements = elements.filter { $0.contains(text) } | |
tableView.reloadData() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment