2

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

jeanmw
  • 446
  • 2
  • 17

4 Answers4

3

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.

jeanmw
  • 446
  • 2
  • 17
2

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

Matt
  • 1,711
  • 18
  • 20
  • I wish I could say this was the answer because it makes perfect sense! But I just made that edit and the cell is still not responding to selection – jeanmw Jan 06 '17 at 05:48
  • You cannot see "hello" printed when clicking the cell? – Matt Jan 06 '17 at 05:52
  • Nope, no printed message @Matt – jeanmw Jan 06 '17 at 05:58
  • as a sanity check for if the search asynchronous function was changing anything, I just tried 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 – jeanmw Jan 06 '17 at 06:15
  • can you comment everything about search bar and check again with just tableview in your view? – Matt Jan 06 '17 at 06:21
  • thanks, @Matt yep, just tried that. I removed all the Search-related code, still no reaction on didSelectRowAt – jeanmw Jan 06 '17 at 06:35
  • I tried your code only with table view and it worked flawlessly. Do you have anything that might block cell interactions? Can you scroll your table? – Matt Jan 06 '17 at 06:41
  • Thanks again @Matt. I can scroll the table, yes. Is that a problem? I can also long press to make the cell appear grey, although it goes back to white when I release it – jeanmw Jan 06 '17 at 06:44
  • 1
    are you using any gestures like tap, pan? – Matt Jan 06 '17 at 06:46
  • 1
    Yes! Found it. UITapGestureRecognizer is used in the extension I wrote to hide the keyboard, called with self.hideKeyboardWhenTappedAround(). Fixing this method remedied the issue. Thanks! – jeanmw Jan 06 '17 at 06:51
1

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

0

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.

Lisa
  • 171
  • 7