如何检测 UITableViewCellStyleSubtitle 样式中 UITableViewCell 对象的 UIImageView 上的触摸
我正在 UITableViewCell 对象“nofollow noreferrer”>UITableViewCellStyleSubtitle
样式(即左侧的图像视图,粗体文本标签,下面是详细文本标签)来创建我的表格。现在我需要检测 UIImageView
上的触摸,并了解单击图像视图的索引路径/单元格。我尝试使用
cell.textLabel.text = @"Sometext";
NSString *path = [[NSBundle mainBundle] pathForResource:@"emptystar1" ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
cell.imageView.userInteractionEnabled = YES;
但它不起作用。每当单击图像时,就会调用 didSelectRowAtIndexPath:
。我不想创建单独的 UITableViewCell
并向其添加自定义按钮。有什么方法可以检测 UIImageView
本身的触摸吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在您的
cellForRowAtIndexPath
方法中添加此代码,然后要检查单击了哪个
imageView
,请检查selector
方法中的标志In your
cellForRowAtIndexPath
method add this codeAnd then to check which
imageView
was clicked, check the flag inselector
method目前已接受的解决方案在 iOS 5.0 中已被破坏。该错误导致图像视图的手势识别器永远不会被触发。通过对官方开发者论坛的研究,我发现这是 iOS 5.0 中的一个已知错误。它是由导致
-gestureRecognizerShouldBegin:
返回 NO 的内部实现引起的。当您将手势识别器的委托设置为自定义UITableViewCell
子类本身时,会出现该错误。修复是重写手势识别器委托中的
-gestureRecognizerShouldBegin:
并返回YES。此错误应在 iOS 5.x 的未来版本中修复。仅当您不使用新的 UITableViewCell 复制/粘贴 API 时,这才是安全的。The accepted solution is currently broken in iOS 5.0. The bug causes the image view's gesture recognizer to never be triggered. Through research on the official developer forums I found that this is a known bug in iOS 5.0. It is caused by an internal implementation that causes
-gestureRecognizerShouldBegin:
to return NO. The bug appears when you are setting the gesture recognizer's delegate to the customUITableViewCell
subclass itself.The fix is to override
-gestureRecognizerShouldBegin:
in the gesture recognizer's delegate and return YES. This bug should be fixed in a future version of iOS 5.x. This is only safe as long as you are not using the new UITableViewCell copy/paste API's.对于 Swift,在你的 cellForRowAtIndexPath 方法中添加此代码
然后检查哪个 imageView 被单击,检查选择器方法中的标志
For Swift, in your cellForRowAtIndexPath method add this code
And then to check which imageView was clicked, check the flag in selector method
一种方法是从图像创建 UIImageView 并向其添加手势识别器。请参阅下面的示例
。但是,您现在必须对单元格进行更多布局,因为您不能简单地使用
UITableViewCell
的UIImageView
属性,因为它是只读的。One way you could do it is by creating a UIImageView from your image and add a gesture recognizer to it. See the example below
However, you'll now have to layout your cells more since you can't simply use the
UIImageView
property of theUITableViewCell
since it's readonly.只是如何附加信息,在我的例子中,我必须响应从 NSFetchedResultsController 检索的表格单元格图像文件上的点击手势,因此我们需要索引路径,而不是行,所以我决定使用点击的图像本身而不是行信息。我为图像定义了一个变量:
我将手势识别器添加到 imageView (imgZona):
并在“imageTapped”方法中检索图像并将其保存到变量“imgSelected”中:
最后,在我发送的“prepareForSegue”方法中viewController 的图像专用于显示点击的图像:
Only how additional information, in my case I had to answer to a tap gesture over an image file of a table cell retrieved from NSFetchedResultsController, so we need the indexPath, not the row, so I decided to use the image tapped itself instead of the row information. I defined a variable for the image:
I added the gesture recognizer to the imageView (imgZona):
and in the "imageTapped" method I retrieved the image and saved to the variable "imgSelected":
Finally, in the "prepareForSegue" method I sent the image of the viewController dedicate to show the image tapped:
这个问题是:
-->下面的代码对我来说可以正常工作:
和“singleTapGestureCaptured”函数
This problem is:
--> The below codes work correctly with me:
and "singleTapGestureCaptured" function