Skip to content

Instantly share code, notes, and snippets.

@runys
Last active September 27, 2018 14:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save runys/7519631e514ef64c24e9cd171d4701a3 to your computer and use it in GitHub Desktop.
Save runys/7519631e514ef64c24e9cd171d4701a3 to your computer and use it in GitHub Desktop.
Search Controller in Table View Controller in iOS 11 with Swift 4. From: https://www.raywenderlich.com/113772/uisearchcontroller-tutorial
// The code bellow must be written in your Table View Controller
// Remember to use your own classes and properties when creating your own search.
// 1. Create a new property in your class
// The Search Controller is the responsible to do the "searching magic"
let searchController = UISearchController(searchResultsController: nil)
// 2. At the viewDidLoad() add those initializations
searchController.searchResultsUpdater = self // You will get an error here for now, but it will vanish at step number 5
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
// 3. Create a propertie in your class to store the results of your filtering
var filteredWords = [String]()
// 4. Create the filter method to filter the objects in your array based on the text the user input
func filterContent(for searchText: String, scope: String = "All") {
filteredWords = words.filter({ word in
return word.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
// 5. Create an extension of your class to implementthe UISearchResultsUpdating protocol
// This protocol is required by the searchController, so it knows what to do when the user
// types the text in the search bar.
extension SuaClasseTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContent(for: searchController.searchBar.text!)
}
}
// 6. Update the method tableView(_:numberOfRowsInSection:)
// so it returns correctly the number of rows when the user is searching
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
return filteredWords.count
}
return words.count
}
// 7. Update the method tableView(_:cellForRowAtIndexPath:)
// so it uses the correct array to display the information
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
let word: String
if searchController.isActive && searchController.searchBar.text != "" {
word = filteredWords[indexPath.row]
} else {
word = words[indexPath.row]
}
cell.textLabel?.text = word
return cell
}
// Now your table view have a beautiful and functional search bar!
// Remember to use your own classes and properties when creating your own search.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment