updateSearchResults() not getting called
Asked Answered
I

4

7

I have read similar problems and solutions on SO. But none seems to solve my problem. I am using Custom Search Controller and Custom Search Bar and func updateSearchResults(for searchController: UISearchController) is not getting called.

    var customSearchController: CustomSearchViewController!

CustomSearchViewController: In ViewDidLoad()

customSearchController = CustomSearchViewController(searchResultsController: ***nil***, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: searchTableView.frame.size.width, height: 44.0), searchBarFont: UIFont(name: "HelveticaNeue", size: 16.0)!, searchBarTextColor: UIColor.purple, searchBarTintColor: UIColor.white)

customSearchController.searchResultsUpdater  = self
customSearchController.definesPresentationContext = true
customSearchController.customSearchBar.placeholder = "What are you looking for?"
customSearchController.customSearchBar.backgroundColor = UIColor.white
customSearchController.customSearchBar.sizeToFit()
customSearchController.customSearchBar.resignFirstResponder()
customSearchController.customSearchBar.showsCancelButton = true
customSearchController.customSearchBar.delegate = self

Not getting called: :(

func updateSearchResults(for searchController: UISearchController) {
    filtered.removeAll()
    filtered = searchArray.filter({ (text) -> Bool in
        let tmp: NSString = text as NSString
        let range = tmp.range(of: customSearchController.customSearchBar.text!, options: NSString.CompareOptions.caseInsensitive)
        return range.location != NSNotFound
    })
    self.searchTableView.reloadData()
}
Impervious answered 10/1, 2017 at 3:46 Comment(1)
Possible duplicate of SearchResultsUpdating not being called in UISearchControllerAccept
I
11

After struggling for hours, I was able to solve it by using: func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) - UISearchBarDelegate delegate method.

instead of updateSearchResults() - UISearchResultsUpdating delegate method

Hope it helps someone :)

Impervious answered 10/1, 2017 at 4:45 Comment(0)
A
7

I had to declare UISearchController instance within the class scope. See this answer. https://mcmap.net/q/1475707/-searchresultsupdating-not-being-called-in-uisearchcontroller

Previously I had declared it within the viewDidLoad method

Accept answered 5/3, 2019 at 5:26 Comment(1)
Yep of course! Otherwise it gets deallocated once it goes out of scopeTrevino
C
4

It looks like you did not set this:

searchController.searchResultsUpdater = self

Make sure this is the last command to avoid getting errors. At least this did the job for me.

Condottiere answered 25/8, 2018 at 13:3 Comment(1)
This is not the answer...see: https://mcmap.net/q/1475707/-searchresultsupdating-not-being-called-in-uisearchcontrollerPantograph
H
0

But maybe your problem was that you call that:

navigationItem.titleView = searchController.searchBar

Instead, you should do that:

navigationItem.searchController = searchController
Hindi answered 14/10, 2018 at 8:31 Comment(3)
Can you provide a reason, why this would help?Pheidippides
Actually, I tried the two cases and I find that the second one works but the first one does not.Hindi
I think the reason is that, in the first case the search controller is not added to the view (just the searchBar) so I can not listen to its changes so the method 'updateSearchResults' will not called.Hindi

© 2022 - 2024 — McMap. All rights reserved.