如何在 UITableViewCellStyleSubtitle 样式中检测对 UITableViewCell 对象的 UIImageView 的触摸

How to detect touches on UIImageView of UITableViewCell object in the UITableViewCellStyleSubtitle style(如何在 UITableViewCellStyleSubtitle 样式中检测对 UITableViewCell 对象的 UIImageView 的触摸)

本文介绍了如何在 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 的触摸

基础教程推荐