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

时间:2011-10-10 11:34:27

标签: objective-c cocoa-touch uitableview uiimageview

我正在使用UITableViewCellStyleSubtitle中的UITableViewCell对象  样式(即左侧的图像视图,粗体的文本标签和详细的文本标签下)以创建我的表格。现在我需要检测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; 

但它不起作用。只要单击图像,就会调用didSelectRowAtIndexPath:。我不想创建单独的UITableViewCell并为其添加自定义按钮。有没有办法检测UIImageView本身的触摸?

6 个答案:

答案 0 :(得分: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];

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

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

答案 1 :(得分:18)

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

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

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

答案 2 :(得分: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)

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

func TappedOnImage(sender:UITapGestureRecognizer){
    println(sender.view?.tag)
}

答案 3 :(得分: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");
}

但是,您现在必须更多地布置您的单元格,因为您不能简单地使用UIImageView的{​​{1}}属性,因为它是只读的。

答案 4 :(得分:2)

只有在我的情况下,我必须回答从NSFetchedResultsController检索到的表格单元的图像文件上的点击手势的额外信息,所以我们需要indexPath,而不是行,所以我决定使用图片轻拍自己而不是行信息。我为图像定义了一个变量:

var imgSelected: UIImageView? = nil

我将手势识别器添加到imageView(imgZona):

public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! ZonasTableViewCell
    let zona = fetchedResultsController.objectAtIndexPath(indexPath) as! Zona

    cell.infoZona = zona

    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))

    cell.imgZona?.addGestureRecognizer(tapGestureRecognizer)
    cell.imgZona?.userInteractionEnabled = true

    return cell
}

并在“imageTapped”方法中检索图像并保存到变量“imgSelected”:

func imageTapped(sender: UITapGestureRecognizer) {

    imgSelected = sender.view as? UIImageView
    self.performSegueWithIdentifier("MuestraImagen", sender: self)
}

最后,在“prepareForSegue”方法中,我发送了viewController专用的图像来显示点击的图像:

override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        switch identifier {
        case "MuestraImagen":
            if let vc = segue.destinationViewController as? MuestraImagenViewController {
                if imgSelected != nil {
                    if let img = imgSelected!.image {
                        vc.imgPuente = img
                    }
                }
            }
        default: break
        }
    }
}

答案 5 :(得分:0)

这个问题是:

  • 将ImageView添加到TableViewCell
  • 事件点击进入ImageView不同于事件点击进入TableViewCell

- >以下代码可以正常使用我:

cell.imageView.image = [UIImage imageNamed:@"my_image.png"];
[cell.imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureCaptured:)];    
[cell.imageView addGestureRecognizer:singleTap];

和“singleTapGestureCaptured”功能

- (void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
    NSLog(@"Left Image clicked");
}