Domanda

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)

È stato utile?

Soluzione

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

Altri suggerimenti

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.

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;  
}

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top