didSelectRowAtIndexPath: not being called

故事扮演 提交于 2019-11-27 02:52:22
Garrett

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.

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()
A. Wilcox

Your problem is case-sensitivity. Your code:

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

should be

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

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;

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

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.

Cancel the other views touches except required one.

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

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

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.

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.

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.

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.

It's work for me, can you try!

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

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