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

Shr*_*hri 30 cocoa-touch objective-c uitableview uiimageview

我正在使用 样式中的UITableViewCell对象UITableViewCellStyleSubtitle(即左侧的图像视图,粗体的文本标签以及详细文本标签下的文本标签)来创建我的表格.现在我需要检测触摸UIImageView并且还要知道单击图像视图的索引路径/单元格.我试过用

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; 
Run Code Online (Sandbox Code Playgroud)

但它不起作用.每当点击图像时,都会didSelectRowAtIndexPath:被调用.我不想创建一个单独的UITableViewCell并添加自定义按钮.有没有办法检测UIImageView自己的触摸?

小智 75

在您的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];
Run Code Online (Sandbox Code Playgroud)

然后检查imageView单击了哪个,检查selector方法中的标志

-(void)myFunction :(id) sender
{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSLog(@"Tag = %d", gesture.view.tag);
}
Run Code Online (Sandbox Code Playgroud)

  • 这在iOS 5.0(也可能是5.0.1)中被打破.见@Andrew的回答 (3认同)

And*_*rew 18

目前已接受的解决方案在iOS 5.0中破坏.该错误导致图像视图的手势识别器永远不会被触发.通过对官方开发人员论坛的研究,我发现这是iOS 5.0中的已知错误.它是由导致-gestureRecognizerShouldBegin:返回NO的内部实现引起的.当您将手势识别器的委托设置为自定义UITableViewCell子类本身时,会出现该错误.

补丁是覆盖-gestureRecognizerShouldBegin:在手势识别的委托,并返回YES.应该在iOS 5.x的未来版本中修复此错误.只要您没有使用新的UITableViewCell复制/粘贴API,这只是安全的.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 我已经实现了这个方法,并将委托设置为self,但没有任何东西可以触发.有谁知道这个问题在iOS 5.1中是什么状态? (5认同)

Esq*_*uth 15

对于Swift,在您的cellForRowAtIndexPath方法中添加此代码

cell.imageView?.userInteractionEnabled = true
cell.imageView?.tag = indexPath.row

var tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
cell.imageView?.addGestureRecognizer(tapped)
Run Code Online (Sandbox Code Playgroud)

然后检查单击了哪个imageView,检查选择器方法中的标志

func TappedOnImage(sender:UITapGestureRecognizer){
    println(sender.view?.tag)
}
Run Code Online (Sandbox Code Playgroud)


aah*_*ens 6

你可以做的一种方法是从你的图像创建一个UIImageView并添加一个手势识别器.请参阅下面的示例

//Create ImageView
UIImageView *theImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"emptystar1.png"]];
theImageView.userInteractionEnabled = YES;

//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageSelectedInTable)];
tapped.numberOfTapsRequired = 1;
[theImageView addGestureRecognizer:tapped];
[cell addSubview:theImageView];

//Memory Cleanup
[tapped release];
[theImageView release];

-(void)imageSelectedInTable
{
    NSLog(@"Selected an Image");
}
Run Code Online (Sandbox Code Playgroud)

但是,您现在必须更多地布置您的单元格,因为您不能简单地使用它自己的UIImageView属性UITableViewCell.

  • Thanx aahrens ..它工作正常.. :)我现在面临的唯一问题是知道哪个图像被选中.有没有办法向@selector(imageSelectedInTable :)发送参数?我使用冒号检查了"id",但是它的UITapGestureRecognizer对象.我需要imageview存在的索引路径.当我尝试@selector(imageSelectedInTable:indexPath)时,应用程序崩溃了.. (2认同)