我正在 UITableViewCell 中使用UITableViewCellStyleSubtitle对象
样式(即左侧的图片 View ,粗体显示的文字标签以及该细部文字标签下方的图片)来创建表格。现在,我需要检测对UIImageView的触摸,还需要了解单击了图像 View 的索引路径/单元格。我尝试使用

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本身的触摸吗?

最佳答案

在您的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,请检查selector方法中的标志
-(void)myFunction :(id) sender
{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSLog(@"Tag = %d", gesture.view.tag);
}

07-24 09:26