didSelectRowAtIndexPath not being called

By Eddy Chung

Is your didSelectRowAtIndexPath function not being called? Here are some possible problems and solutions - I’ll list them from the most common to least common.

Delegate Not Set

Have you set your UITableViewDelegate for the table view? This can be set in the storyboard or in code.

To set it up in code:

tableView.delegate = self

You can also do it in the Storyboard by dragging the delegate outlet to the view controller: did select row at index path not being called 2020 01 22 13 19 20

After you drag it you should see this in your outlet inspector:

did select row at index path not being called 2020 01 22 13 22 12

If you have a .XIB file you can do something similar by dragging the outlet to the File's Owner.

did select row at index path not being called 2020 01 22 13 27 42

It should look like this after you have done it:

did select row at index path not being called 2020 01 22 13 28 21

Check The Method Name didSelect

Did you accidentally use the method name didDeselect instead of didSelect?

It seems like an obvious mistake, but it happens pretty often! The two method names are quite similar.

Xcode sometimes autocompletes didDeselect before didSelect.

Double-check if that’s the case, if not, keep on reading.

Allow Selection For Table View

The next thing to check is if selection has been enabled for the table view.

You can check this in the storyboard or XIB file by selecting the tableview and going to the attribute inspector:

did select row at index path not being called 2020 01 22 13 30 53

Make sure Selection is set to Single Selection or Multiple Selection. Not No Selection.

You can also make this change in Swift code:

tableview.allowsSelection = true

Or to allow multiple selection:

tableview.allowsMultipleSelection = true

Is There a UITapestureRecognizer

Have you created a UITapGestureRecognizer on top of your table view?

It’s possible that the UITapGestureRecognizer is swallowing your click events before they reach your table view.

To resolve this, set the cancelsTouchesInView property of the UITapGestureRecognizer to false.

You can also set this in your storyboard by selecting the UITapGestureRecognizer, navigating to the property inspector and unchecking this box:

did select row at index path not being called 2020 01 22 13 36 53

Is User Interaction Enabled

Make sure that user interaction is enabled on your table view.

You can set this in Swift code by doing:

tableview.isUserInteractionEnabled = true

Or in the interface builder by selecting the table view and opening up the attribute inspector:

did select row at index path not being called 2020 01 22 13 42 42

Disable User Interaction On A View Inside The Table View

Perhaps you have created a button or another user element in your table view cell.

It is possible that it is “stealing” the clicks of your table view cell.

Try to turn off user interaction on that element and see if your didSelectRowAtIndexPath function gets called.

You can do this in code:

viewInsideTableViewCell.isUserInteractionEnabled = false

Or by unchecking the box in the interface builder. Simply select the element and navigate to the attribute inspector:

did select row at index path not being called 2020 01 22 13 44 41

Did You Create A Custom Content View

Did you create a custom content view and name it contentView?

This can be problematic because the default content view of a cell is also named contentView.

This can result in odd behavior. It’s best that you name your custom view something else, like tableCellContentView or anything else that isn’t contentView.

Check The Visual Debugger

Hopefully one of the above solutions worked for you!

If not the last thing I would check is the visual debugger.

Run your app in the simulator and then click this icon:

did select row at index path not being called 2020 01 22 13 50 57

This will open up the visual debugger, where you can use your mouse to click and drag.

Check if any views (possibly transparent) are in front of your table view and preventing the clicks.

did select row at index path not being called 2020 01 22 13 51 58

Hopefully one of these solutions worked for you.

If you liked this post, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts