Yetispapa
Yetispapa

Reputation: 2296

'didSelectRowAt' is not being called

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?

Upvotes: 25

Views: 18848

Answers (13)

Mariano Guadagnini
Mariano Guadagnini

Reputation: 1

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.

Upvotes: 0

Sultan Ali
Sultan Ali

Reputation: 2589

check you didSelectRowAt method may be you have typo didDeselectRowAtIndexPath

There is a chance you accidentally typed didDeselectRowAtIndexPath

Upvotes: 1

Haseeb Javed
Haseeb Javed

Reputation: 2045

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

Upvotes: 0

Bavafaali
Bavafaali

Reputation: 398

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

Upvotes: 1

Aman.Samghani
Aman.Samghani

Reputation: 2381

There might be the following issue

s:

  1. You have not setup the delegate of the UITableView


self.tableview.delegate = self
  1. Check if the user-interaction is no disable for UITableView or UITableViewCell


self.tableview.isUserInteractionEnabled = true
  1. Check if the tableview selection style is not none


self.tableview.allowsSelection = true
  1. Check if any of the gestures are not enabled.


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true
}

Upvotes: 10

Martin
Martin

Reputation: 4765

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!

Upvotes: 3

Mostfa Essam
Mostfa Essam

Reputation: 937

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

storyboard

Upvotes: 22

Paul Paul
Paul Paul

Reputation: 41

I suggest writing replace this on your code.

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

Upvotes: 4

Miravzal
Miravzal

Reputation: 2035

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

Upvotes: 12

igor
igor

Reputation: 761

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

Upvotes: 50

Elsa
Elsa

Reputation: 193

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)

Upvotes: 1

Lal Krishna
Lal Krishna

Reputation: 16160

Please check TableView's cell allowsSelection.

tableView.allowsSelection = true

Edit:

Check:

  • tableView.allowsSelection

  • cell.selectionStyle

  • User Interaction Enabled

Upvotes: 37

Fares Benhamouda
Fares Benhamouda

Reputation: 619

I dont know what others solutions you tried, but if you are using a custom viewcell , it might be that you didint enable "User Interaction Enabled" on that component.

Upvotes: 2

Related Questions