2

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()

    }
}
Pete
  • 613
  • 1
  • 6
  • 18
  • For your first problem 'a)', are you testing in the Simulator or a device? If in the Simulator, try tapping ⌘K (command-k). – benhameen May 27 '15 at 19:38
  • 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 May 27 '15 at 19:39
  • 1
    benhameen - thanks I was able to get the keyboard with the shortcut. – Pete May 27 '15 at 19:40
  • ...but the second issue remains – Pete May 27 '15 at 20:04
  • can you post the code of cellForRowAtIndexPath? – Praveen Gowda I V Jun 06 '15 at 10:59
  • Due to the fact that I use a second class for the Search it does not work. I found out that if I use the same OverviewTableViewController for the search results it accesses the same Table View and thus the same cells. So I don't make a resultsSearchController but rather use the following: searchController = UISearchController(searchResultsController: nil) – Pete Jun 06 '15 at 12:27

3 Answers3

6

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.

C0D3
  • 6,440
  • 8
  • 43
  • 67
0

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.

Andriy Gordiychuk
  • 6,163
  • 1
  • 24
  • 59
0

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

searchController.definesPresentationContext = true

Natan R.
  • 5,141
  • 1
  • 31
  • 48