Search code examples
swiftuitableviewdidselectrowatindexpathfirst-responder

didSelectCellAtIndexPath not called


I know there are a lot of relevant post on this and I have tried all of the ones I can find and unfortunately the problem still persists.

So I have a tableView inside of an UIViewController which is populated by 2 custom cells, one has got an UITextField and the other one got an UISwitch, and both have a UILabel. I then proceeded to implement the didSelectCell(at: IndexPath) method

I made sure the delegate is hooked up to the view controller.

Inside of the method, I simply wrote a print statement to ensure the taps are registering. However, the print message did to get printed to the console when I tapped on the UITextField and on the UILabel.

I have a feeling it is something to do with the firstResponder thing with the UITextField, and it is eating away my tap registration, but not really sure.

Any feedbacks are welcome!

UPDATE

Here is the setup on my custom cell:

enter image description here

Here is the didSelect method:

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


        print("tapped on cell")


        if let cell = tableView.cellForRow(at: indexPath) as? NumericInputTableViewCell{
            print("Tapped on numericcell")
        } else if let cell = tableView.cellForRow(at: indexPath) as? BooleanInputTableViewCell {
            print("Tapped on booleancell")
        }

    }

Solution

  • The delegate method is triggered when you click on the cell, not other elements on the cell. So create some empty cell in your table view and have another try. If it get printed out then that means you have elements covered over the cell and the cell itself is not clicked.

    You can then disable user interaction for your UITextField stuffs from storyboard and then have a try. Ideally, when you click on the textfield on a table view cell, the correct respond I am thinking is that keyboard will show up and didSelectCell should not be triggered.

    Here is the demo

    enter image description here