yhl
yhl

Reputation: 689

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?

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)

Upvotes: 1

Views: 1378

Answers (4)

Alan Silva
Alan Silva

Reputation: 138

Check in the interface builder if the property selection is set to 'No selection'. Change it to 'Single selection' or other option according to your needs.

This might be the reason why didSelect is not getting triggered.

Upvotes: 0

Frederic Caplet
Frederic Caplet

Reputation: 1

To answer to the question "how I can get the UITapGestureRecognizer working on the same screen as the UITableView";

You should "inform" your GestureRecognizer that other Recognizer can handle the same gesture:

You do that by implementing the method of UIGestureRecognizerDelegate

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

This is a small exemple...

@interface MyController : UIViewController<UIGestureRecognizerDelegate>  
{ 
}  
@end  


-(void)viewDidLoad  
{  
    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(actionOnTapGesture:)];  
    [gr setNumberOfTapsRequired:1];  
    [gr setDelegate:self];  
    [self.view addGestureRecognizer:gr];    
}  

-(BOOL) gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyGestureRecognizer:(UIGestureRecognizer *) otherGestureRecognizer  
{  
    return YES;  
}

Upvotes: 0

Schrodingrrr
Schrodingrrr

Reputation: 4271

have you set the delegate of the tableView?

myTableView.delegate = self;

EDIT: My bad, did not read that cell for row is being called.

You say that you have used a custom tableViewController. If you have overridden the didSelectRowAtIndexPath method, it might be important to call [super didSelectRowAtIndexPath:] in the tableViewController

EDIT 2: One more thing. I do not know the reason for this, but I faced the same issue some time back in a viewController. I resolved it by adding an empty implementation of didDeselectRowAtIndexPathin the same viewController. Try adding it to your table's delegate controller.

Upvotes: 1

yhl
yhl

Reputation: 689

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

Upvotes: 2

Related Questions