'didSelectRowAt' is not being called

Viewed 14320

i have a question about the UITableView delegate function didSelectRowAt. Everything is working fine but unfortunately the didSelectRowAt is not called. I read in some other stackoverflow question about the problem and tried some solutions out but none of them works for me. I made a subclass of the tableview which is the delegate himself:

class MyTableView : UITableView, UITableViewDelegate{

    override func awakeFromNib() {

        super.awakeFromNib()

        separatorStyle = .none

        backgroundView = nil
        backgroundColor = UIColor.clear

        isScrollEnabled = false

        delegate = self
        isEditing = false
        allowsSelection = true

    }


//    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
//       
//    this is working        
//
//    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print("not called")

    }    

} 

So all subclasses of MyTableView will implement the datasource stuff and this also working fine (in case somehow will mentioned this).

The strange thing is that didHighlightRowAt is called, so the delegate somehow works. Only the didSelectRowAt which I want is not being called.

By the way there aren't any UITapGestureRecognizer.

Can somehow give me any advice here. Are there properties which are wrong?

13 Answers

If you have a parent view with gesture recognizer assigned to it you'll need to do something like this:

self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: action)
if let tapGestureRecognizer = self.tapGestureRecognizer {
  tapGestureRecognizer.cancelsTouchesInView = false
  self.addGestureRecognizer(tapGestureRecognizer)
}

Once you told the gesture recognizer to stop canceling touches, your UITableView starts reacting on taps as normal

For me, changing selection style, from none, solved the problem.

storyboard

If your ViewController has UIGestureRecognizer You should disable UIGestureRecognizer.cancelsTouchesInView. For example:

 extension UIViewController {

    public func hideKeyboarOnTap() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboardAction))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
    }

    @objc private func hideKeyboardAction() {
        self.view.endEditing(true)
    }
}

I suggest writing replace this on your code.

override func viewDidLoad() {
    super.viewDidLoad()
    tableview.delegate = self
}

I added a mechanism to hide keyboard when tapping out of text field:

    ...

    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap(recognizer:)))
    tapRecognizer.numberOfTapsRequired = 1
    self.view.addGestureRecognizer(tapRecognizer)
}

@objc func handleSingleTap(recognizer: UITapGestureRecognizer) {
    self.view.endEditing(true)
}

The handleSingleTap stole the event. I removed everything above, and used this instead:

override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    view.endEditing(true)
}

And editing came back!

check you didSelectRowAt method may be you have typo didDeselectRowAtIndexPath

There is a chance you accidentally typed didDeselectRowAtIndexPath

I the same problem, even though the delegate was setup correctly. In my case, it was not possible to interact with the table view because it was not the fist subview from the user's perspective. You can make sure it is displayed in front by using this method when you add it to your main view:

view.insertSubview(yourTableView, at: self.view.subviews.count)

or bring it to the front using this:

self.view.bringSubview(toFront: yourTableView)

For me some views was blocking each other and because i've set tableView:shouldHighlightRowAt: to false i wasn't able to detect the problem

Objective C - Possible Solutions:

  1. You just have to use:

    tap.cancelsTouchesInView = NO;

Code:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                           initWithTarget:self
                           action:@selector(yourFunctionOnTap)];
tap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap];
  1. Set allow selection property of tableview

tableView.allowsSelection = true

I had the same issue. A tableview, embedded in a container view that, in turn was collapsable (via a UIPanGestureRecognizer).

Turned out that, in addition to tap.cancelsTouchesInView = false and the delegate's shouldRecognizeSimultaneouslyWith, also had to set tap.delaysTouchesBegan = true.

Apparently, some sort of race condition between all the recognizer's around.

Related