Search code examples
swiftuinavigationcontrolleruisearchbar

Swift 3: press outside searchBar, search is automatic deleted


Configured a searcher in the navigation bar in a UITable. Everything is working fine, accept the fact that when I searched and found the needed cells and I want to click on that cell, the search term is quickly removed from the search bar. The result is that I get back the "old" table. I can not use the search results.

Class property: UITableViewController, ExpandableHeaderViewDelegate, UISearchBarDelegate, UISearchResultsUpdating {

    var filteredArray = [Mijnproducten]()
    var shouldShowSearchResults = false

override func viewDidLoad() {
        super.viewDidLoad()


         configureSearchController()

        }

    func configureSearchController() {
        // Initialize and perform a minimum configuration to the search controller.
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = true
        searchController.searchBar.placeholder = "Search here..."
        searchController.searchBar.sizeToFit()
        searchController.hidesNavigationBarDuringPresentation = false

        // Place the search bar view to the tableview headerview.
        self.navigationItem.titleView = searchController.searchBar
    }


    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        shouldShowSearchResults = true
        searchWasCancelled = false
        self.tableView.reloadData()
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        shouldShowSearchResults = false
        searchWasCancelled = true
        self.tableView.reloadData()

    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        var searchTerm = ""

        if searchWasCancelled {
            searchController.searchBar.text = searchTerm
        }
        else {
            searchTerm = searchController.searchBar.text!
        }
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        let text = searchController.searchBar.text
        searchController.searchBar.text = text
        if !shouldShowSearchResults {
            shouldShowSearchResults = false
            self.tableView.reloadData()
        }

        searchController.searchBar.resignFirstResponder()
    }

    func updateSearchResults(for searchController: UISearchController) {

        let searchtext = searchController.searchBar.text
        print(searchtext)

        filteredArray = mijnproducten01.filter{ product in
            return product.productname.lowercased().contains((searchtext?.lowercased())!)
        }
        print(filteredArray)

        if searchtext?.isEmpty == false
        { shouldShowSearchResults = true }
        else
        { shouldShowSearchResults = false }

        // Reload the tableview.
        self.tableView.reloadData()
        }

    }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

      let indexPath = tableView.indexPathForSelectedRow
      let currentCell = tableView.cellForRow(at: indexPath!) as! RowTableViewCell
      let RowName = currentCell.ProductLine!.text
      let temp = currentCell.UUID!.text
        print("jaaaa", temp)
      TAGID = Int(temp!)!


        if RowName == "History"
        {
            tableView.deselectRow(at: indexPath!, animated: true)
            self.performSegue(withIdentifier: "PropertyToHistory", sender: self)
        }
        else if RowName == "Description"
        {
            tableView.deselectRow(at: indexPath!, animated: true)
            self.performSegue(withIdentifier: "PropertyToDescription", sender: self)
        }
        else if RowName == "Transfer"
        {
            tableView.deselectRow(at: indexPath!, animated: true)
            self.performSegue(withIdentifier: "PropertyToTransfer", sender: self)
        }
     }

Solution

  • I would suggest you to put breakpoints on your searchBarTextDidBeginEditing,searchBarCancelButtonClicked,searchBarTextDidEndEditing,searchBarSearchButtonClicked and updateSearchResults. When you select the cell and search result disappears then see where the breakpoint stops.

    It seems that there could be logic issues with shouldShowSearchResults and searchWasCancelled.

    Because from what you are saying is correct then somehow your searchController.searchBar.resignFirstResponder() is triggered which cancels the search. Putting up breakpoints and debugger will get you the culprit.