Cannot Select TableViewCell when Search Bar is active - iOS Swift

1.6k views Asked by At

I have a search bar that is connected to a table view. It works exactly as I want when the search bar is not active but cell selection is disabled when the search bar is active. I've debugged it and didSelectRowAtIndexPath is not even being called when I select a row when search is active. What could be causing this?

Here's the relevant code:

class FabricsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate, UISearchDisplayDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        searchController.delegate = self
        searchController.searchBar.delegate = self
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        searchController.definesPresentationContext = false
        searchController.hidesNavigationBarDuringPresentation = false
        myTableView.tableHeaderView = searchController.searchBar

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if searching {
            searching = false
            searchBar?.resignFirstResponder()
            FirebaseClient.sharedInstance.joinFabric(uid: self.appDelegate.uid!, fabricKey: allFabrics[indexPath.row].key)
            updateFabricList()
        } else {
             appDelegate.selectedFabricKey = joinedFabrics[indexPath.row].key
             performSegue(withIdentifier: "fabricSelected", sender: self)
        }
        myTableView.deselectRow(at: indexPath, animated: false)
    }

    func filterContentForSearchText(searchText: String, scope: String = "All") {
        if let allFabrics = allFabrics {
            filteredFabrics = allFabrics.filter { fabric in
                return (fabric.name.lowercased().contains(searchText.lowercased()))
            }
            myTableView.reloadData()
            myTableView.setContentOffset(CGPoint.zero, animated: false)
        }
    }

}

extension FabricsViewController: UISearchResultsUpdating {
    func updateSearchResults(for: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }
}
2

There are 2 answers

0
Anusha On
  1. Please check the text for your method name. Make sure that you are using method name as below:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

  1. You have set the delegate of searchBar to self. Are you activating any tap gestures when search becomes active? This tap gesture can interrupt your didSelectRowAtIndexPath method.
0
dskibin On

For those who are facing same issue, according Apple's documentation you should have following code for add searchController:

if #available(iOS 11.0, *) {
   navigationItem.searchController = searchController
} else {
    self.yourTableView.tableHeaderView = searchController.searchBar
}