Cannot select TableViewCell when search bar is active - iOS Swift

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

Here is 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!)
    }
}
+4
source share
1 answer
  • Please check the text for the name of your method. Make sure you use the method name as shown below:

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

  1. searchBar . , ? didSelectRowAtIndexPath.
0

Source: https://habr.com/ru/post/1664713/


All Articles