UILabel with Gesture Recognizer inside UITableViewCell blocks didSelectRowAtIndexPath

冷暖自知 提交于 2019-12-25 01:13:26

问题


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

}


来源:https://stackoverflow.com/questions/26392775/uilabel-with-gesture-recognizer-inside-uitableviewcell-blocks-didselectrowatinde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!