Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a UITableViewController conform to protocol UISearchResultsUpdating?

I have a UITableViewController class in which I am implementing a UISearchController. I've added the following delegates:

class EmployeesTableView: UITableViewController, NSFetchedResultsControllerDelegate,UISearchResultsUpdating{

I'm importing both UIKit and CoreData. I'm getting the following error:

"Type 'CustomTableViewController' does not conform to protocol UISearchResultsUpdating"

What do I need to do to make the controller conform to the protocol?

like image 731
krisacorn Avatar asked Feb 15 '15 02:02

krisacorn


3 Answers

Swift 3:

func updateSearchResults(for searchController: UISearchController) {

// code here

}
like image 137
Grant Fleming Avatar answered Nov 08 '22 08:11

Grant Fleming


When you add protocols to your class definition, the easiest way is to mouse over the protocol name and command click its name. This will pull up its definition. With protocol definitions, they usually have methods immediately following them. If a method is required it will be at the top, if it has optional in front, then it isn't required in order to conform.

In the case of `UISearchResultsUpdating, it only has one method and it is required. Just copy the method, or multiple methods and click the back arrow to get back to your class. Paste the methods into your class, and implement them. If they were optional methods (in this case there are no optional methods), remove optional from the front. This is what I copied from the definition.

func updateSearchResultsForSearchController(searchController: UISearchController)

Then you update it to do what you want to do.

func updateSearchResultsForSearchController(searchController: UISearchController) {
    //do whatever with searchController here.
}

As an additional example, command click on NSFechedResultsControllerDelegate. You will see that it has no required methods, but lots of optional ones. This information is usually found in the documentation as well, but I've found command + click to be the fastest way to find what I'm looking for.

like image 42
Jeremy Pope Avatar answered Nov 08 '22 09:11

Jeremy Pope


Swift 3.0

//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!)
     }
}
like image 6
Ben Avatar answered Nov 08 '22 09:11

Ben