Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long press only in didSelectRowAtIndexPath

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")
}
like image 571
LateNate Avatar asked Oct 06 '16 22:10

LateNate


1 Answers

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.

like image 53
LateNate Avatar answered Nov 15 '22 19:11

LateNate