didSelectRowAtIndexPath: not being called

2020-01-27 00:13发布

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.

13条回答
啃猪蹄的小仙女
2楼-- · 2020-01-27 00:51

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;
查看更多
相关推荐>>
3楼-- · 2020-01-27 00:57

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'.

查看更多
放我归山
4楼-- · 2020-01-27 01:00

Cancel the other views touches except required one.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
    if (touch.view == your view) {
        return YES;
    }
    return NO;
}
查看更多
孤傲高冷的网名
5楼-- · 2020-01-27 01:02

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.

查看更多
来,给爷笑一个
6楼-- · 2020-01-27 01:03

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.

查看更多
我想做一个坏孩纸
7楼-- · 2020-01-27 01:04

Your problem is case-sensitivity. Your code:

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

should be

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
查看更多
登录 后发表回答