How to detect touches on UIImageView of UITableViewCell object in the UITableViewCellStyleSubtitle style(如何在 UITableViewCellStyleSubtitle 样式中检测对 UITableViewCell 对象的 UIImageView 的触摸)
问题描述
我在 UITableViewCell 对象" rel="nofollow noreferrer">UITableViewCellStyleSubtitle
样式(即左侧的图像视图,粗体文本标签和详细文本标签下方)来创建我的表格.现在我需要检测对 UIImageView
的触摸,还需要知道单击图像视图的索引路径/单元格.我尝试使用
I am using the UITableViewCell
object in the UITableViewCellStyleSubtitle
style (i.e an image view on the left, text label in bold and under that a detail text label) to create my table. Now I need to detect touches on the UIImageView
and also to know the indexpath/cell in which the image view was clicked. I tried using
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
本身的触摸?
But it's not working. Whenever the image is clicked, didSelectRowAtIndexPath:
is called. I don't want to create a separate UITableViewCell
and add a custom button to it. Is there any way to detect touches on the UIImageView
itself?
推荐答案
在你的 cellForRowAtIndexPath
方法中添加这段代码
In your cellForRowAtIndexPath
method add this code
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
[tapped release];
然后要检查哪个 imageView
被点击,检查 selector
方法中的标志
And then to check which imageView
was clicked, check the flag in selector
method
-(void)myFunction :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(@"Tag = %d", gesture.view.tag);
}
这篇关于如何在 UITableViewCellStyleSubtitle 样式中检测对 UITableViewCell 对象的 UIImageView 的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 UITableViewCellStyleSubtitle 样式中检测对 UITableViewCell 对象的 UIImageView 的触摸
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01