我在 UITableViewCellStyleSubtitle
中使用 UITableViewCell
对象
样式(即左侧的 ImageView ,粗体文本标签和详细文本标签下方)来创建我的表格。现在我需要检测 UIImageView
上的触摸,还需要知道单击 ImageView 的索引路径/单元格。我尝试使用
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);
}
关于objective-c - 如何检测 UITableViewCellStyleSubtitle 样式中 UITableViewCell 对象的 UIImageView 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7712161/