-2

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")
        }

    }
Brendon Cheung
  • 995
  • 9
  • 29

1 Answers1

1

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

Fangming
  • 24,551
  • 6
  • 100
  • 90
  • But with that logic, tapped on rows in the setting on an iPhone shouldn't work too because you are tapping on a `UILabel` not the cell? – Brendon Cheung Sep 21 '17 at 14:17
  • @BrendonCheung No. It's all about views. You have views one over another one and in your case you lower view (tableViewCell) is registered for click event. If the view over it is also registered for some event, like a text field, then it will trigger that instead of the cell click event on the view below. If the view over it does not have click event, then your click will *pass through* it and directly get to the cell. By default, images and labels does not have click event and that's why the didSelect delegate method works. And by default a text field has a click event. – Fangming Sep 21 '17 at 14:22
  • @BrendonCheung Here is the best way to test it out. click on the cell and hold your finger on the screen. If the cell is selected, you will see the animation that the cell is being clicked. Then when you release your finger, the `didSelect` method will be called. – Fangming Sep 21 '17 at 14:24
  • Hi, when I hold down the click, the `didSelect` is not called, I only have two things inside of that cell, a `UITextField` and a `UILabel` – Brendon Cheung Sep 21 '17 at 14:33
  • @BrendonCheung Just attached demo to my answer. The method will not be triggered unless the cell is released. But look at the animation of the cell. Only after this animation can you be sure that the cell is actually clicked, not some other elements over it – Fangming Sep 21 '17 at 14:37
  • Thanks for that demo! I have long clicked on the cell not the elements inside it and it didn't register as a tap... – Brendon Cheung Sep 21 '17 at 14:42
  • @BrendonCheung That's the problem. Either your cell is set not to be clicked on from storyboard, or you have some element covers it. So find that out and your problem should be fixed – Fangming Sep 21 '17 at 14:44