Search code examples
iosuisearchdisplaycontroller

IOS display array of last 10 search string when click at UISearchBar


I'm I'm developing an app with UICollectionViewController. I'm implementing a search feature with UISearchController. Here is what I have done:

var searchController: UISearchController!

override func viewDidLoad() {
        searchController = UISearchController(searchResultsController: nil)
    searchController.delegate = self
    searchController.searchBar.delegate = self
    searchController.searchResultsUpdater = self
    searchController.searchBar.placeholder = "Search for article"
    searchController.hidesNavigationBarDuringPresentation = false
    self.navigationItem.titleView = searchController.searchBar
    searchController.searchBar.sizeToFit()
    definesPresentationContext = true
}

The problem is that how can we display list of 10 recent search string whenever user click on the UISearchBar? and detect if the user click on any previous search result (To reload collectionView to show filtered result).


Solution

  • You have to implement it on your own. Whenever user click on the UISearchBar a delegate is called and you can show a UITableView attached beneath it listing 10 recent searches (if there's any).

    When user searches something, add it to your maintained recent searches array/queue.

    If user clicks on any recent searched item i.e. on UITableView, didSelectRow delegate will be called, you can search that string and reorganize the recent searches array/queue.