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.
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:
After you drag it you should see this in your outlet inspector:
If you have a .XIB
file you can do something similar by dragging the outlet to the File's Owner
.
It should look like this after you have done it:
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.
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:
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
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:
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:
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 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
.
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:
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.
Hopefully one of these solutions worked for you.
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.