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 的触摸
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
