Nat
Nat

Reputation: 1762

UISearchResultsUpdating not conforming to extension class in view controller

I'm trying to get something like instagrams explore page, example here (image is too large for adding it onto the post). I have an explore viewController with a search bar at the top and a collection view for the content underneath the search bar, whenever the user clicks the search bar it'll go to my search table view controller class which gives them the results of users that match their query and vice versa.

Here's my normal search view controller (the main one where it shows the content):

import UIKit

class SearchViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let SearchResultsViewController = storyboard.instantiateViewController(withIdentifier: "SearchResultsViewController") as? SearchTableViewController else { return}

    let searchController = UISearchController(searchResultsController: SearchResultsViewController)

    searchController.searchResultsUpdater = SearchTableViewController // ERROR HERE

    view.addSubview(searchController.searchBar)
    definesPresentationContext = true
}
}

and here's my search table view controller:

class SearchTableViewController: UIViewController {

let languages = ["Mandarin Chinese", "English", "Hindustani", "Spanish", "Arabic", "Malay", "Russian", "Bengali", "Portuguese", "French", "Hausa", "Punjabi", "German", "Japanese", "Persian", "Swahili", "Telugu", "Javanese", "Wu Chinese", "Korean"]
var searchResults: [String] = []

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.dataSource = self
}

}

extension SearchTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
    guard let searchText = searchController.searchBar.text else { return }
    searchResults = languages.filter { $0.contains(searchText) }
    tableView.reloadData()
}
}

extension SearchTableViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchResults.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath)

    let language = searchResults[indexPath.row]

    cell.textLabel?.text = language
    return cell
}
}

At the moment I'm stuck on calling the searchResultsUpdating class. With an error of Cannot assign value of type 'SearchTableViewController.Type' to type 'UISearchResultsUpdating?'. My SearchTableViewController has the UISearchResultsUpdating delegate in the extension of the class, I don't know where I've gone wrong? I'm trying to achieve something like Pete's answer with this design:

enter image description here

Obviously without the tab navigation bar.

Upvotes: 0

Views: 389

Answers (1)

emrepun
emrepun

Reputation: 2666

Dont you need an instance of SearchTableViewController?

You are trying to assign class type instead of an instance of it here:

searchController.searchResultsUpdater = SearchTableViewController

You need to either initalize SearchTableViewcontroller or have a reference to an instance of it to set as searchResultsUpdater

Upvotes: 1

Related Questions