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 - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01