2
votes

I am using the UISearchController and the search function works except for two things. Perhaps they are related: a) The keyboard does not show. So I cannot press the "Search" button b) In my main TableView I access the prototype cell that has the style "Subtitle". When the search is going on, the cell that shows is "Basic" style. In both cases (TableViewController and SearchViewController) I use the same cell identifier.

Any ideas? Here some of the code:

class OverviewTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let resultsController = SearchResultsController()
        resultsController.birds = birds
        searchController = UISearchController(searchResultsController: resultsController)
        //searchController.dimsBackgroundDuringPresentation = true

        let searchBar = searchController.searchBar
        searchBar.scopeButtonTitles = ["One", "Two"]
        searchBar.placeholder = "Search"
        searchBar.sizeToFit()
        tableView.tableHeaderView = searchBar
        searchController.searchResultsUpdater = resultsController

and the second class:

class SearchResultsController: UITableViewController, UISearchResultsUpdating {

    var items: [Item] = []
    var filtered: [Item] = []

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

// MARK: - UISearchResultsUpdating Conformance

func updateSearchResultsForSearchController(searchController: UISearchController) {
    //searchController.hidesNavigationBarDuringPresentation = true
    let searchString = searchController.searchBar.text
    let buttonIndex = searchController.searchBar.selectedScopeButtonIndex
    filtered.removeAll(keepCapacity: true)

    if !searchString.isEmpty {
        for item in items {
            ...
            }
        }

    self.tableView.reloadData()

    }
}
3
For your first problem 'a)', are you testing in the Simulator or a device? If in the Simulator, try tapping ⌘K (command-k).benhameen
Also, could you show us some relevant code so that we can help you a bit more? Perhaps some of the delegate/datasource methods of both controllers?benhameen
benhameen - thanks I was able to get the keyboard with the shortcut.Pete
...but the second issue remainsPete
can you post the code of cellForRowAtIndexPath?Praveen Gowda I V

3 Answers

6
votes

I had the same issue and basically if your parent class has a UISearchController as well as your child class and you're setting the

self.definesPresentationController = YES

in your parent class, your child class' search controller won't have the keyboard showing. Took me a while to figure out but this is what is happening.

The way to fix it is to set the self.definesPresentationController = NO when your parent class is about to push the child class and set self.definesPresentationController = YES in the viewWillAppear of your parent class...

Hope this helps someone out there too.

0
votes

In my main TableView I access the prototype cell that has the style "Subtitle". When the search is going on, the cell that shows is "Basic" style

Make sure that cells in the SearchResultsController are also of "Subtitle" style.

0
votes

Besides doing what the other users suggested, I also did the following, and it worked:

searchController.definesPresentationContext = true