Long press only in didSelectRowAtIndexPath
Asked Answered
L

1

11

I've asked a similar question, and I've read countless other questions but I still can't figure this out and I've spent a few nights now trying to solve it.

I have a custom UITableView class which I have subclassed to call different things. In my subclasses I user overrides on things like didSelectRowAtIndexPath.

If I assign this to a table it works fine, except I have to do a long press in order to fire off didSelectRowAtIndexPath. Tap doesn't seem to work at all. I do have a gesture recogniser elsewhere in the project but I've disabled them all and I've still had no luck getting tap working. I've read this might be something to do with supers or touchesbegan but I feel like I'm completely stuck now. Please help me!

class BaseTable: UITableView, UITableViewDelegate, UITableViewDataSource {

var rowsInSection: Int { return  0}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    //Standard table set up funcs

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

    {
    print(IndexPath.row)
    }
}

class NewTable: BaseTable{

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {                    
        print (IndexPath.row + " NEW")
}
Larsen answered 6/10, 2016 at 22:55 Comment(10)
Follow the link you will find the answer #28431163Watercourse
@Joe, unless I'm missing something this seems to be an unrelated issue. My problem is I can't get didSelectRowAtIndexPath to occur from a single tap. Instead it seems to occur when it detects a long press.Larsen
Is your table view inside a scroll view?Linen
No @Sam_M, this is a tableview inside a viewcontroller. Ive assigned a tableview subclass to it. There is no gesture recogniser in the viewcontroller either.Larsen
If you still using UIGeatureRegogniserDelegate in your class.please take if off .see that fixes the problemWatercourse
Unfortunately I don't have this in my project at all.Larsen
I need more detail.please update your code and let me know,where you getting the data from...Watercourse
Do you have the same problem when you don't use your subclass but use UITableView instead?Glyptodont
@Glyptodont No. When I set the table up within the ViewController everything worked fine. Its only when I used the subclass that I lost the tap functionality.Larsen
Oh, I've just realised that's not what you meant. Yes - If I use the BaseTable UITableView class I still lose the tap functionality.Larsen
L
35

I figured this out. I'd forgotten I had a UIViewController extension that dismissed the keyboard on tap:

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)
    }

This was the culprit! Quick fix was to add:

        tap.cancelsTouchesInView = false

right before adding the gesture recogniser. Can't beleive I spent 3 nights on this.

Larsen answered 7/10, 2016 at 14:15 Comment(7)
life saver. This is a big help!Stoichiometry
you are my heroSubtype
If you find this issue, check for any GestureRecognizer methods in your class. It will be the one causing this issue.Muldon
I found this issue since 4 hours, Thanks you save my more time from wasting. :)Moiramoirai
also helped me a lot! Thanks @LarsenLibeler
If I could give you 10 upvotes I would. Been trying to fix this for days. Finally googled the right thing.Olethea
Amazing!!! You are Awesome.Orchidaceous

© 2022 - 2024 — McMap. All rights reserved.