307

I'm writing an iOS app with a table view inside a tab view. In my UITableViewController, I implemented -tableView:didSelectRowAtIndexPath:, but when I select a row at runtime, the method isn't being called. The table view is being populated though, so I know that other tableView methods in my controller are being called.

Does anyone have any ideas what I may have screwed up to make this happen?

5
  • 84
    also you may have a gestureRecognizer on top of the UITableView that absorbs the selection .. (one of the possibilities)
    – M.Othman
    Nov 10, 2012 at 7:47
  • Be careful if the table view is being populate, it means the DataSource is well set. The selection is part of the Delegate methods. So maybe the DataSource is well set but not the Delegate ! Aug 8, 2013 at 16:32
  • 1
    Hi M.Othman, your comment is exactly what was wrong with my own issue. Do you know how to get gestureRecognizer working 'with' the UITableView?
    – yhl
    Sep 30, 2013 at 1:01
  • 15
    I notice that if I tap and hold, the tap eventually calls -didSelectRowAtIndexPath. I figured out hat's because I have a tap gesture recognizer on the tableview and the gesture has to fail first before receiving the touch. Though it's weird the table selection animation is not affected by the gesture recognizer.
    – Hlung
    Nov 22, 2013 at 4:47
  • 6
    I had the same problem here and had the fortunate opportunity to learn that you cannot have a gestureRecognizer on top of the UITableView. Thanks M. Othman!
    – vnchopra
    Jan 2, 2014 at 19:15

60 Answers 60

535

Just in case someone made the same stupid mistake as I did:

Check out if the method name of what you expect of being didSelect may accidentally be gotten didDeselect in some way. It took about two hours for me to find out ...

2
  • 47
    Same issue here. It's because XCode autocompletes the Deselect before the Select. Jun 5, 2013 at 17:09
  • Similarly, I had set allowsSelection = false, which as well as removing any selection highlight, also prevents the cell from being pressed!
    – djinne95
    Oct 24, 2019 at 13:31
520

Another thing that might lead to the issue is not selected selection kind:

UITableView selection kind

Should be Single Selection for normal selection, should not be No Selection.

To do this programmatically, do:

tableView.allowsSelection = YES
5
  • 4
    I keep running into this because I have scenes that are always in edit mode and I keep forgetting to change the default from "No Selection During Editing".
    – Symmetric
    Dec 14, 2012 at 22:50
  • 3
    You can add this line in your -viewDidLoad method of viewControlller NSParameterAssert(self.tableView.allowsSelection); Feb 16, 2015 at 15:37
  • for those who observed tableView.rx.itemSelected.subscribe didnt call back, this is the issue and above fix works
    – Dhilip
    May 17, 2018 at 13:52
  • so helpful when you take over some codebase and it has code using buttons with tags and not didSelectRow, with this selection disabled in the storyboard 🙄 Jan 30, 2019 at 14:19
  • is this tableView default behaviour? Nov 4, 2019 at 8:24
303

Another possibility is that a UITapGestureRecognizer could be eating the events, as was the case here: https://stackoverflow.com/a/9248827/214070

I didn't suspect this cause, because the table cells would still highlight blue as if the taps were getting through.

6
  • This was my problem! Thank you so much!! I mean it clearly worked before, but it stopped working after I implemented UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; Jul 4, 2013 at 21:31
  • 29
    I had the same problem as you @coolcool1994 just implemented [tap setCancelsTouchesInView:NO]; and solved the issue.
    – nizx
    Jan 9, 2014 at 4:42
  • Thank, it helps me. In my case didSelectRowAtIndexPath doesn't works only after delete cell in commitEditingStyle. Then I make UITapGestureRecognizer and in tapAction: got indexPath from sender.view ([self.tableView indexPathForCell:sender.view]) and call [self.tableView.delegate didSelectRowAtIndexPath:myIndexPath]
    – Alexmelyon
    Apr 18, 2014 at 8:20
  • 1
    This was the my issue too .. add code to remove the tapgesture in tableview & it was work like charm - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init]; gesture.cancelsTouchesInView = NO; [self.tableView addGestureRecognizer:gesture]; }
    – Udaya Sri
    Aug 20, 2014 at 7:24
  • 7
    Thanks @nizx, in swift 4 tap.cancelsTouchesInView = false Apr 24, 2018 at 11:14
151

All good answers, but there's one more to look out for...

(Particularly when creating a UITableView programmatically)

Make sure the tableView can respond to selection by setting [tableView setAllowsSelection:YES]; or removing any line that sets it to NO.

2
  • 11
    I'd add setAllowsSelectionDuringEditing: to the list (when tableview is in editing mode)
    – alex-i
    Oct 29, 2013 at 11:33
  • Thank you! In IB I had to change in the "Selection" section value to Single Selection.
    – Sasho
    Jul 31, 2017 at 6:02
99

It sounds like perhaps the class is not the UITableViewDelegate for that table view, though UITableViewController is supposed to set that automatically.

Any chance you reset the delegate to some other class?

4
  • 7
    I discovered that while my controller was a UITableViewController, I used a plain old UIViewController widget in UI Builder. That doesn't work. When I deleted the UIViewController widget and dropped a UITableViewController in its place, everything worked. Nov 1, 2008 at 20:51
  • 1
    Another boundary case for everyone - I wired up the delegate in code and had forgotten I had previously wired the delegate via the storyboard .... last one to set it wins.
    – Oly Dungey
    Aug 6, 2014 at 12:34
  • 1
    can you show how to "reset a delegate to some other class" please? I have this same issue and can't manage to solve it
    – Alfro
    Jul 14, 2016 at 8:19
  • 1
    myTableViewController.tableView.delegate = xxx
    – Hunter
    Jul 18, 2016 at 0:12
95

If the problem arise with UITapGestureRecognizer you can fix this:

  • in Storyboard:

enter image description here

in code with Objective-C:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
[self.view addGestureRecognizer:tap];

[tap setCancelsTouchesInView:NO];

in code with Swift:

let tap = UITapGestureRecognizer(target: self, action:Selector("dismissKeyboard"))
view.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false
0
68

I have encountered two things in this situations.

  1. You may have forgot to implement UITableViewDelegate protocol, or there's no delegation outlet between your class and your table view.

  2. You might have a UIView inside your row that is a first responder and takes your clicks away. Say a UIButton or something similar.

2
  • 5
    It started happening on iOS 7, disabling "User Interaction Enabled" of views within contentView fixed this.
    – Kof
    Oct 3, 2013 at 12:18
  • 1
    @Kof nailed it. Creating a UITableViewCell in Xcode 5's interface builder creates it with user interaction enabled = YES. Bad Xcode 5!
    – jsd
    Oct 10, 2013 at 0:12
46

I have had the same problem. And it was hard to find. But somewhere in my code was this:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

It must be return indexPath, else -tableView:didSelectRowAtIndexPath: is not being called.

2
  • 2
    for did SelectRow At IndexPath correct signature is: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Jul 4, 2013 at 21:23
  • For me, it wouldn't start calling tableView:didSelectRowAtIndexPath until I deleted willSelectRowAtIndexPath entirely
    – etayluz
    Jun 30, 2015 at 16:13
40

If you added a gestureRecognizer on top of the UITableView, didSelectRowAtIndexPath will not get called.

So you need to use gestureRecognizer delegate method to avoid touch in particular view.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:YourTable]) {
        return NO;
    }
    return YES;
}
1
  • 3
    if ([touch.view isDescendantOfView:YourTable])
    – Sudheesh
    Apr 13, 2016 at 10:04
30

I've ran into a problem where after months of not looking at my code I forgot that I implemented the following method due to some requirements which were not necessary

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath  *)indexPath{
    return NO;
}

It should be returning YES for a row in order to make it selected.

0
24

YOU MUST select these options

enter image description here

but if you want to make UITableViewnot highlighted on clicking then you should make changes in UITableViewCell properties.

Choose None option for Selection just like below

enter image description here

0
21

I had put a UITapGestureRecognizer on my table view to dismiss the keyboard which prevented didSelectRowAtIndexPath: from being called. Hope it helps someone.

1
  • Saved me - I had two gestures, one for longpress which I removed forgetting that I also had one for keyboard dismissal Nov 28, 2020 at 16:44
19

In case you have the same problem as me: Apparently, this method won't be called if your tableView is in edit mode. You have to set allowsSelectionDuringEditing to true.

Via this question: When editing, `UITableView` does not call didSelectRowAtIndexPath ??

19

I had the same problem,

The reason was using of UITapGestureRecognizer. I wanted the keyboard to dismiss when I tapped anywhere else. I realized that this overrides all tap actions, that is why, didSelectRowAtIndexPath function did not called.

When I comment the rows related with UITapGestureRecognizer, it works. Moreover you can check in the function of UITapGestureRecognizer selector if the tapped is UITableViewCell or not.

0
14

In my case, didSelctRowAtIndexPath not calling is due to that I have selected none in the Selection property of tableView, set to single selection solved my problem

enter image description here

0
11

For Xcode 6.4, Swift 1.2 . The selection "tag" had been changed in IB. I don't know how and why. Setting it to "Single Selection" made my table view cells selectable again. enter image description here

10

Even though another answer has been accepted, I'll add one more possible problem and solution for people who observe this issue:

If you have automatic reference counting (ARC) turned on, you may find that even after assigning your controller as a delegate of the view, the view's messages to the controller are not being received because ARC is deleting the controller. Apparently the UITableView's delegate pointer does not count as a reference for the ARC, so if that is the only reference to it, the controller will be dealloc'd. You can verify whether or not this is happening by implementing the dealloc method on the controller and setting a breakpoint or NSLog call there.

The solution is to keep track of the controller with a strong reference somewhere else, until you are sure you won't need it anymore.

3
  • The controller being garbage-collected while its view is still displayed? Can this actually happen?
    – Drux
    Jul 29, 2014 at 10:02
  • @Drux Yep! When this happened to me it was nearly the first version of Obj-C with garbage collection used on iOS and honestly they didn't do a great job. A lot of references were explicitly or implicitly "weak" that, IMO, should have been strong. Which led to oddities like garbage collection of objects in active use by the display layer. I haven't done iOS work in a while so I have no idea if this still happens on the latest version. Jul 29, 2014 at 15:52
  • My code has growing memory size problem and I fixed somewhat of it and somewhere else in the code just after this didSelect wont get called the first time only. I don't know does this justify the problem or not.
    – Amber K
    May 7, 2018 at 15:44
10

Remember to set the datasource and delegate in the viewDidLoad method as follows:

[self.tableView setDelegate:self];

[self.tableView setDataSource:self];
1
  • I was calling datasource but not delegate... thanks!
    – Gmeister4
    May 27, 2016 at 9:39
9

My problem was none of the above. And so lame. But I thought I would list it here in case it helps someone.

I have a tableViewController that is my "base" controller and then I create subclasses of this controller. I was writing all my code in the tableView:didSelectRowAtIndexPath routine in the "base" class. Completely forgetting that by default this routine had also been created (albeit with no code that did anything) in all of my subclasses as well. So when I ran my app, it ran the subclass version of the code, did nothing, and made me sad. So of course, once I removed the routine from the subclasses, it used mt "base" class routine and I'm in business.

I know. Don't laugh. But maybe this will save someone the hour I lost...

0
9

Giving my 2 cents on this.

I had a Custom UITableViewCell and there was a button covering the whole cell, so when the touch happened, the button was selected and not the cell.

Either remove the button or in my case, I set User Interation Enable to false on the button, that way the cell was the one selected.

8

If you read this, so still doesn't solve the problem.

I have custom cell, where checkbox "User Interaction Enabled" was disable. So, I Just switch on it. Good luck.

7

I just had this and as has happened to me in the past it didn't work because I didn't pay attention to the autocomplete when trying to add the method and I actually end up implementing tableView:didDeselectRowAtIndexPath: instead of tableView:didSelectRowAtIndexPath:.

7

Make sure you implemented tableView:didSelectRowAtIndexPath and not tableView:didDeSelectRowAtIndexPath

This is gotten me on more than a few occasions !!

6

Please check for UITapGestureRecognizer. In my case tapgesture was added for the view where tableview got placed, which is eating the user interaction of UITableview like didselect. After disabling tapgesture for the view, didselect delegate was triggered.

1
  • This was exactly my problem, using UITableView in a ViewController.
    – JoeGalind
    Sep 29, 2019 at 3:11
5

If your table view is in editing mode (eg. [tableView setEditing:YES animated:NO];), you need to set tableView.allowsSelectionDuringEditing = YES;

5

In my case the solution was to change NO to YES in the below function.

iOS 9+

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
1
  • This was my problem too. I have a multiple tableView implementation and forgot to separate the two tableViews in this method. Oct 23, 2017 at 13:54
5

Check if your viewController has following method:

- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

if you return "NO" didSelectRow won't be called

1
  • I spent the whole of yesterday trying to figure this out. Thank you. Jun 19, 2019 at 17:23
4

I know is old and the problem was resolved, but a had similar problem, I thought that the problem was with my custom UITableViewCell, but the solution was completely different - I restart XCode :) and then works ok ! almost like Windows :)

4

Another mistake you could've done (as I did): if you set a segue at the cell, didSelectRowAtIndexPath is not called. You should set your segues at the view controller instead.

4

None of these answers worked for me. After about an hour, I figured out something very insidious:

I have a table view inside a cell of another table view. I decided to make an enclosing view that contains the inner table view, among other things. I called this view contentView and hooked it up in the xib.

Turns out that UITableViewCell already has a contentView and does weird things with it. The issue resolved itself when I renamed the property to mainContentView and reconnected the view to this renamed property.

Not the answer you're looking for? Browse other questions tagged or ask your own question.