Search code examples
iosuitableviewdidselectrowatindexpath

didSelectRowAtIndexPath does not trigger in ViewController


So, I've added a UITableViewController and UITableViewCell into a UIViewController and while the cellForRowAtIndexPath delegate method works, didSelectRowAtIndexPath does not. Does anyone have any ideas?

  • EDIT 2: The delegate for the UITableView is set to the UIViewController.

  • EDIT 3: I found the answer to my issue on another Stack question here. Basically I had a UITap... on my [self view] that was blocking the didSelectRow.... I have no idea why the tap blocks the delegate method and I also have no idea how to get both the tap and the table working together simultaneously.

  • EDIT: The part that bugs me is that I've gotten this exact setup working on an earlier app. So that means I've missed a step somewhere along the way. The thing is, I've combed over all the steps and have compared previous app vs current app and I really have no idea what I missed.

I've added logging to both delegate methods and while one outputs, the other does not.

ViewController.h

#import "...TableViewCell.h"
...
    UITableViewDataSource,
    UITableViewDelegate
...

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath");
    ...
    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath");
}

...TableViewCell.h (contents not important)

...TableViewCell.m (contents not important)


Solution

  • I found the answer on another StackOverflow question.

    I had a UITapGestureRecognizer added to [self view] which I commented out, and then the delegate method worked.

    Can anyone please tell me why this worked and also how I can get the UITapGestureRecognizer working on the same screen as the UITableView?

    // Hide keyboard when user taps outside of it
    UITapGestureRecognizer *tapGestureRecognizer =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(hideKeyboardOnTap)];
    //[[self view] addGestureRecognizer:tapGestureRecognizer];
    
    • EDIT: Corrected typo of UITapeGestureRecognizer to UITapGestureRecognizer