UILabel with Gesture Recognizer inside UITableView

2019-09-08 07:47发布

问题:

I use some UILabels with a UITapGestureRecognizer inside a UITableViewCell. The GestureRecognizer works well. But when I tap on the label, I want that the didSelectRowAtIndexPath: should execute too. Or even just the indexPathForSelectedRow() method should give me the selected row.

Setting cancelsTouchesInView = false did not work!

Is this possible? Right now the indexPathForSelectedRow() method returns nil. Thanks

回答1:

Why are you using UITapGestureRecognizer? If you want to use that, try to set the tag of label as label.tag=indexpath.row. So you might get the value you are looking at. Regarding my own opinion, I'd remove the uitapgesturerecognizer and directly use didselectrowatindexpath method..

EDIT 2:

Try using this solution..it might help you..

 -(void)handleTap:(UITapGestureRecognizer *)sender

   {

CGPoint location = [sender locationInView:self.view];

    if (CGRectContainsPoint([self.view convertRect:self.yourTableView.frame fromView:self.tableView.superview], location))
    {
        CGPoint locationInTableview = [self.yourTableView convertPoint:location fromView:self.view];
        NSIndexPath *indexPath = [self.yourTableView indexPathForRowAtPoint:locationInTableview];
        if (indexPath)
            [self tableView:self.yourTableView didSelectRowAtIndexPath:indexPath];

        return;
    }

}