krisacorn
krisacorn

Reputation: 831

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?

Upvotes: 6

Views: 8252

Answers (3)

Ben
Ben

Reputation: 596

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

Upvotes: 6

Grant Fleming
Grant Fleming

Reputation: 191

Swift 3:

func updateSearchResults(for searchController: UISearchController) {

// code here

}

Upvotes: 18

Jeremy Pope
Jeremy Pope

Reputation: 3352

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.

Upvotes: 9

Related Questions