Can't get didSelectRowAt to work for TableView

1k Views Asked by At

I am having trouble getting the didSelectRowAt method to work for a TableView inside of a regular ViewController. I have already made sure that the delegate and data source for the table are set in the ViewController code. This ViewController populates the tableview cells with results from a search query to an API, and the rendering of cell data is working fine.

It's just the didSelectRowAt method that is not registering. I did try manually adding the same delegate information on the Main.storyboard, but the little + sign won't trigger any popup windows. I am wondering if there is something in the Main.storyboard that needs fixing. I have attached the images of the ViewController and TableView connections inspector as well. I am new to iOS development and don't have much experience with graphic interfaces for mobile design, so I am assuming it's something there but maybe I am wrong.

Here's the basic version of my code:

class SearchViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var tableView: UITableView!
@IBOutlet var searchBar: UISearchBar!

   ...variable declarations ....

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    searchResults = []
    searchBar.delegate = self
    tableView.dataSource = self
    tableView.delegate = self

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1;
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchResults!.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "searchTableViewCell", for: indexPath) as! SearchTableViewCell

    if(searchActive && !(self.searchResults?.isEmpty)!) {
        (doing some stuff with search results here...works fine)
    }
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  print("hello!")
}

func searchBar(_ searchBar: UISearchBar,
               textDidChange searchText: String) {
    print("search text \(searchText)")
    getSearchResultJSON(term: searchText) { json in
        let data = json as! [Dictionary<String, String>]
        self.searchResults = data
    }
    self.tableView.reloadData()
    }
...
}

[Viewcontroller connections inspector]

[TableView Connections Inspector]

EDIT: as a sanity check for if the search asynchronous function was changing anything, I just tried removing all search-related code and filling the tableview from a hardcoded dummy variable array. It worked to display the dummy variables, but still no ability to select a cell and get any reaction. I also saw a couple mentions that I had previously had a typo with didDeSelectRowAt instead of didSelectRow at, that has been fixed but the behaviour is the same.

This ended up being related to a tap gesture that occurs in the hideKeyboardWhenTappedAround() extension that I wrote

4

There are 4 best solutions below

0
jeanmw On BEST ANSWER

Found it! The culprit was the self.hideKeyboardWhenTappedAround(), which is an extension I wrote to hide the keyboard. This interfered with the tap of a cell because it did indeed utilize UITapGestureRecognizer. Thanks for the hints everyone.

10
Matt On

You are using didDeselectRowAt instead of didSelectRowAt

Edit

Well, use this below delegate then

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

and make your controller conform to UIGestureRecognizerDelegate

0
puran thapa On

If you are using tap gesture on main view then table view cell did select method is not working properly.

0
Lisa On

In picture you uploaded, delegate and datasource aren't connected to the ViewController. Remove the code in viewdidload (tableview.delegate = self) and connect them in storyboard.