问题描述
我在
样式(即左侧的图像视图,粗体的文本标签,文本标签)来创建我的表。现在我需要检测 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
方法中添加此代码
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
,检查选择器
方法中的标志
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上的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!