在 UITableViewCell 块中带有手势识别器的 UILabel 块 didSelectRowAtIndexPath [英] UILabel with Gesture Recognizer inside UITableViewCell blocks didSelectRowAtIndexPath
问题描述
我在 UITableViewCell
中使用了一些带有 UITapGestureRecognizer
的 UILabels.GestureRecognizer 运行良好.但是当我点击标签时,我希望 didSelectRowAtIndexPath:
也应该执行.或者甚至只是 indexPathForSelectedRow()
方法应该给我选定的行.
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.
设置 cancelsTouchesInView = false
不起作用!
这可能吗?现在 indexPathForSelectedRow()
方法返回 nil.谢谢
Is this possible? Right now the indexPathForSelectedRow()
method returns nil.
Thanks
推荐答案
你为什么使用 UITapGestureRecognizer
?如果要使用它,请尝试将标签的标签设置为 label.tag=indexpath.row
.所以你可能会得到你正在寻找的价值.关于我自己的意见,我会删除 uitapgesturerecognizer
并直接使用 didselectrowatindexpath
方法..
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..
编辑 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;
}
}
这篇关于在 UITableViewCell 块中带有手势识别器的 UILabel 块 didSelectRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!