【发布时间】:2016-12-08 22:54:39
【问题描述】:
我有一个自定义的 tableviewCell,里面有一个 UIImageView imgView。我在 imgView 中添加了UITapGestureRecognizer,这样我可以在点击时更改 imgView 的图像。但是,当我点击 imgView 时,didSelectRowAtIndexPath 也会被触发。我想要的是:
- 在imgView外部点击时:触发
didSelectRowAtIndexPath - imgView 内部点击时:阻止
didSelectRowAtIndexPath,只需调用手势回调即可更改 imgView 的图像。
当我在 imgView 中点击时已经调用了回调函数,但在这种情况下我无法阻止 didSelectRowAtIndexPath。我一直在寻找几个小时,但还没有找到解决方案。我正在使用objective-c。有人对我的问题有任何想法吗?谢谢大家!
更新
我终于知道我的错误是什么了。
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onFavouriteIconTapped:)];
tapGesture.cancelsTouchesInView = NO; // this should be YES by default
[_ivImageView addGestureRecognizer:tapGesture];
只需设置cancelsTouchInView = YES 即可解决我的问题。如果值为 YES,触摸事件将被 ivImageView 消耗,并且不会选择单元格。希望这能帮助遇到同样问题的人。
【问题讨论】:
-
尝试添加 UIButton 和操作而不是 UITapGesture 和 UIImageView。
-
添加您正在使用的语言的标签。 Obj C 或 Swift。
-
这应该有助于UIGesture & UITableView
-
aad UIButton on imageView 并在@iphonic 说的操作方法中编写代码
-
我更新了我的问题。我的问题不在于调用手势功能。该函数已被调用。我的问题是:如何阻止 didSelectRowAtIndexPath 以防在 imgView 内部点击(如果在 imgView 外部点击,仍保留 didSelectRowAtIndexPath)
标签: objective-c ios objective-c uitableview uitapgesturerecognizer didselectrowatindexpath