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")
}
LateNate
  • 763
  • 9
  • 22
  • Follow the link you will find the answer http://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift – Joe Oct 06 '16 at 23:05
  • @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. – LateNate Oct 06 '16 at 23:18
  • Is your table view inside a scroll view? – Swifty Oct 06 '16 at 23:19
  • 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. – LateNate Oct 06 '16 at 23:20
  • If you still using UIGeatureRegogniserDelegate in your class.please take if off .see that fixes the problem – Joe Oct 06 '16 at 23:37
  • Unfortunately I don't have this in my project at all. – LateNate Oct 06 '16 at 23:51
  • I need more detail.please update your code and let me know,where you getting the data from... – Joe Oct 07 '16 at 00:07
  • Do you have the same problem when you don't use your subclass but use UITableView instead? – charmingToad Oct 07 '16 at 04:23
  • @charmingToad 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. – LateNate Oct 07 '16 at 07:42
  • 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. – LateNate Oct 07 '16 at 08:20

1 Answers1

33

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.

LateNate
  • 763
  • 9
  • 22