didSelectRowAtIndexPath: not being called

不羁的心 提交于 2019-11-26 10:20:16

问题


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.


回答1:


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.




回答2:


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()



回答3:


Your problem is case-sensitivity. Your code:

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

should be

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



回答4:


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;



回答5:


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




回答6:


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.




回答7:


Cancel the other views touches except required one.

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



回答8:


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




回答9:


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.




回答10:


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.




回答11:


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.




回答12:


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.




回答13:


It's work for me, can you try!

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



来源:https://stackoverflow.com/questions/8952688/didselectrowatindexpath-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!