60
votes

I have a UITableView as a subview of my UIScrollVIew, which is the main view controlled by my MainViewController.

In MainViewController.h

@interface MainViewController : UIViewController <UIGestureRecognizerDelegate, UITableViewDelegate, UITableViewDataSource>

// other stuff here...

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

In MainViewController.m

@synthesize myTableView;

// other stuff here...

- (void)viewDidLoad {
    myTableView.delegate = self;
    myTableView.datasource = self;
}

// other stuff here...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
   [self performSegueWithIdentifier:@"listAttributesSegue" sender:self];
}

I know that didSelectRowAtIndexPath is not being called because I have set breakpoints on both the method itself and the line of code inside it, and neither is being called. I also know that the datasource is working correctly because I have other functions which modify the cells at runtime and they are working perfectly fine. I am using the latest Xcode with iOS 5.0 set as the development target. I have searched and searched for an answer. Anyone have any ideas?

Edit: I have found the answer. I had a UITapGestureRecognizer set for myTableView's superView. This overrode the selection call. Credit to whoever suggested that that might be it. Your answer was deleted before I could mark it correct.

Edit 2: A lot of people have been commenting about this, so I though I would share it. If you are experiencing this problem, simply set myGestureRecognizer.cancelsTouchInView to false and everything should work fine.

14
Did you implement tableView:willSelectRowAtIndexPath:? Maybe it's returning nil thus preventing didSelectRowAtIndexPath: from calling? - Kyr Dunenkoff
Thank you for returning with your solution. I think it could be better to put the edit as an answer. - Lorenzo B
I will but I can't answer my own question for at least 8 hours after asking it. - Garrett
I was afraid other events were eating the callback process. This confirmed and was able to fix. Thanks! - OneChillDude

14 Answers

309
votes

I have found the answer. I had a UITapGestureRecognizer set for myTableView's superView. This overrode the selection call. Credit to whoever suggested that that might be it. Your answer was deleted before I could mark it correct.

Set the cancelsTouchesInView property to NO on the gesture recogniser to allow the table view to intercept the event.

16
votes

Updated for Swift 3:

if you are used UITapGestureRecognizer in your code :- # Swift 3 use below lines of code:

extension YourViewController{
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
        tap.cancelsTouchesInView = false
    }

    func dismissKeyboard() {
        view.endEditing(true)
    }
}

How to called:- In ViewDidLoad()

self.hideKeyboardWhenTappedAround()
10
votes

Your problem is case-sensitivity. Your code:

- (void)tableVIew:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

should be

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
7
votes

Have you defined instance variable for tableview with same name. If not then might be this can be the issue-

_myTableView.delegate = self;
_myTableView.datasource = self;

Or-

self.myTableView.delegate = self;
self.myTableView.datasource = self;
7
votes

Maybe it is a typo after all. Check that your function is not didDeselectRowAtIndexPath: (de select instead of select).

7
votes

My solution is :

  1. set cancelsTouchesInView To No of any tapGesture

  2. I found in my custom cell , userInteractionEnable is set to NO, simply delete userInteractionEnable = No and issue solved.

3
votes

Cancel the other views touches except required one.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == your view) {
        return YES;
    }
    return NO;
}
1
votes

Sorry, haven't got enough points to add comments - Garret's answer is great but I would add:

You can still have your gesture recognizer but you will need to set 'Cancels touches in view' to NO - then the gestures will be handed on to the view and your UITableView will work fine.

After trying many, many approaches this seems to be the correct way of doing things: a tap gesture recognizer with 'cancel touches in view' is like having an invisible layer on top of everything that grabs all the events and routes them to the view controller (the proxy). The view controller then looks at the gesture to see if it has an action binding (buttons etc.) and will route those and any remaining will just go to the gesture handler. When using a UITableView it is expecting to receive the tap but the view controller snaffles it when you have 'Cancels touches in view'.

1
votes

I was having this issue for a while, and I did not see any reference to it here, so for reference, another reason for this could be that:

tableView.editing = YES;

but

tableView.allowsSelectionDuringEditing = NO;

As per documentation for

- tableView:didSelectRowAtIndexPath:

This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode). See "Managing Selections" in Table View Programming Guide for iOS for further information (and code examples) related to this method.

1
votes

My case is strange. My tableView has 2 sections. 1st section's cells work fine about tableView:didSelectRowAt:, but 2nd section's cells doesn't trigger didSelectRowAt:.

The above problem happens at iPhone 4s, iOS 9.3. But in iPhone 5s, iOS 10.3, there are no problems, those cells works fine. It seems like iOS 9 bugs about UITableView.

After many tests, I found out one line codes produces this bug.

tableView.estimatedSectionHeaderHeight = 60.0

Because the 2nd sections has no header view. I remove this line, and all works fine.

0
votes

A cell can be selected by the user (tapping on the row), by calling "tableView.selectRowAtIndexPath(..)" or "cell.setSelected(true, ...).

  • If the cell is selected by calling "cell.setSelected(true)", the user cannot deselect the cell anymore.

  • If the cell is selected by calling "tableView.selectRowAtIndexPath()", the user can deselect the cell as expected.

0
votes

I had intermittent failure of didSelectRowAtIndexPath: being called on my custom cell press.

I discovered that if I stopped calling [tableView reloadData] very often (10 Hz), and changed it to update every 2 seconds, almost every press would successfully call didSelectRowAtIndexPath:

It seems like reloading the view blocks presses.

0
votes

My problem is the cell is a customized cell, and the action does not work on it. In addition, there is a UITapGestureRecognizer defined in the superclass.

Firstly, Set tapGesture.cancelsTouchesInView = false

    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(endEditing))
        tapGesture.cancelsTouchesInView = false
        view.addGestureRecognizer(tapGesture)
    }

Secondly, Instead of setting isUserInteractionEnabled = true; in the table view, I set the action on the cell.

In the ViewDidLoad()

        super.viewDidLoad()
        
        tableView.delegate = self
    }

Then in the

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell: UITableView = tableView.dequeueReusableCell(for: indexPath)
        cell.isUserInteractionEnabled = true;

You can try this solution if you are creating a customized cell.

-1
votes

It's work for me, can you try!

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap)