objective-c - 如何检测 UITableViewCellStyleSubtitle 样式中 UITableViewCell 对象的 UIImageView 上的触摸

标签 objective-c cocoa-touch uitableview uiimageview

我在 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/

相关文章:

iphone - 如何将应用程序的铃声添加到 iPhone 的铃声中?

ios - 将NSInteger添加到NSMutableArray

ios - 如何使用 CGAffineTransformMakeRotation?

ios - 在显示/隐藏键盘 iOS11 上滚动 UITableView

ios - 更改我的 NSString 中特定字符的字体系列

iphone - 为 UITableView 添加阴影

ios - 标签栏( View Controller )中调用多次ios的通知中心方法方法?如何删除观察者?

iphone - 如果使用其他 NSMutableArray 初始化,如何更新 NSMutableArray

ios - 先执行代码! swift

ios - 为单个 UITableViewCell 设置分隔符插图