Albin Martinsson
Albin Martinsson

Reputation: 81

UITableView didSelectRowAtIndexPath only recognize taps with two fingers

I'm attempting to present a UIAlertController when a UITableViewCell is selected. I have checked all the storyboard-related issues such as Selection: Single Selection and even set tableView.allowsSelection = true at the top of viewDidLoad(). I also checked the issues of using didDeselect as opposed to didSelect.

Oddly enough the cell only selects when I tap it with two fingers at the same time or when I press it for a longer period of time (about 2s and then release). Is this a known problem?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("Tap registered")
}

Any amount of help would be greatly appreciated.

Upvotes: 8

Views: 1204

Answers (3)

Haseeb Javed
Haseeb Javed

Reputation: 2054

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];

Upvotes: 0

Rattanakoudom Sambath
Rattanakoudom Sambath

Reputation: 514

Since this question issue for a long time, so I'm gonna leave my solution here for others who face this problem, as I use this method and it works fine. Here are my codes :

For tableViewCell

First, declare a variable, we check null to avoid crash on the table

var tapOnCell: (() -> Void)?

Then inside awakeFromNib block, so here we create an UITapGestureRecognizer for tap action, the gesture will require us to tap 1 time on cell.

    let gesture = UITapGestureRecognizer(target: self, action: #selector(tapOnContenView))
    gesture.numberOfTapsRequired = 1
    contentView.addGestureRecognizer(gesture)

Then create a function like below

@objc func tapOnContenView() {
    if let tapOnCell = self.tapOnCell {
        tapOnCell()
    }
}

So we finish here on tableViewCell now it's time for tableView

For tableView (UITableViewDataSource)

call function that we create on tableViewCell and use it here

cell.tapOnCell = {
        self.tapOnSingleCell(indexPath: indexPath)
    }

but first, we need to create the function for action that you want to perform when you click on the cell, for me, I want to go to another viewController when I click the cell.

func tapOnSingleCell(indexPath: IndexPath) {

    let jobDetail: JobDetailViewController = 
JobDetailViewController.newInstance(storyboardName: .jobSearch)
    let job = self.relatedList[indexPath.row]
}

I hope this solution help you as well as it help me, cheer.

Upvotes: 1

art
art

Reputation: 51

I had similar problem but saw other posts noting that a UIGesture Recognizer attached to parent view was the problem. I had done just that. The Gesture recognizer was used to dismiss the keyboard. So for what it is worth make sure you take care when using an UIGestureRecognizer in a UITableView.

Upvotes: 2

Related Questions