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

发布于 2024-12-08 21:56:08 字数 809 浏览 0 评论 0 原文

我正在 UITableViewCell 对象“nofollow noreferrer”>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; 

但它不起作用。每当单击图像时,就会调用 didSelectRowAtIndexPath:。我不想创建单独的 UITableViewCell 并向其添加自定义按钮。有什么方法可以检测 UIImageView 本身的触摸吗?

I am using the UITableViewCell object in the UITableViewCellStyleSubtitle
style (i.e an image view on the left, text label in bold and under that a detail text label) to create my table. Now I need to detect touches on the UIImageView and also to know the indexpath/cell in which the image view was clicked. I tried using

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; 

But it's not working. Whenever the image is clicked, didSelectRowAtIndexPath: is called. I don't want to create a separate UITableViewCell and add a custom button to it. Is there any way to detect touches on the UIImageView itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娇妻 2024-12-15 21:56:08

在您的 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);
}

In your cellForRowAtIndexPath method add this code

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];

And then to check which imageView was clicked, check the flag in selector method

-(void)myFunction :(id) sender
{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSLog(@"Tag = %d", gesture.view.tag);
}
葬心 2024-12-15 21:56:08

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

修复是重写手势识别器委托中的-gestureRecognizerShouldBegin:并返回YES。此错误应在 iOS 5.x 的未来版本中修复。仅当您不使用新的 UITableViewCell 复制/粘贴 API 时,这才是安全的。

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

The accepted solution is currently broken in iOS 5.0. The bug causes the image view's gesture recognizer to never be triggered. Through research on the official developer forums I found that this is a known bug in iOS 5.0. It is caused by an internal implementation that causes -gestureRecognizerShouldBegin: to return NO. The bug appears when you are setting the gesture recognizer's delegate to the custom UITableViewCell subclass itself.

The fix is to override -gestureRecognizerShouldBegin: in the gesture recognizer's delegate and return YES. This bug should be fixed in a future version of iOS 5.x. This is only safe as long as you are not using the new UITableViewCell copy/paste API's.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}
温暖的光 2024-12-15 21:56:08

对于 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)
}

For Swift, in your cellForRowAtIndexPath method add this code

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

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

And then to check which imageView was clicked, check the flag in selector method

func TappedOnImage(sender:UITapGestureRecognizer){
    println(sender.view?.tag)
}
以往的大感动 2024-12-15 21:56:08

一种方法是从图像创建 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");
}

。但是,您现在必须对单元格进行更多布局,因为您不能简单地使用 UITableViewCellUIImageView 属性,因为它是只读的。

One way you could do it is by creating a UIImageView from your image and add a gesture recognizer to it. See the example below

//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");
}

However, you'll now have to layout your cells more since you can't simply use the UIImageView property of the UITableViewCell since it's readonly.

作业与我同在 2024-12-15 21:56:08

只是如何附加信息,在我的例子中,我必须响应从 NSFetchedResultsController 检索的表格单元格图像文件上的点击手势,因此我们需要索引路径,而不是行,所以我决定使用点击的图像本身而不是行信息。我为图像定义了一个变量:

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
        }
    }
}

Only how additional information, in my case I had to answer to a tap gesture over an image file of a table cell retrieved from NSFetchedResultsController, so we need the indexPath, not the row, so I decided to use the image tapped itself instead of the row information. I defined a variable for the image:

var imgSelected: UIImageView? = nil

I added the gesture recognizer to the 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
}

and in the "imageTapped" method I retrieved the image and saved to the variable "imgSelected":

func imageTapped(sender: UITapGestureRecognizer) {

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

Finally, in the "prepareForSegue" method I sent the image of the viewController dedicate to show the image tapped:

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
        }
    }
}
柏林苍穹下 2024-12-15 21:56:08

这个问题是:

  • 将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");
}

This problem is:

  • Add ImageView into TableViewCell
  • Event click into ImageView different than event click into TableViewCell

--> The below codes work correctly with me:

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];

and "singleTapGestureCaptured" function

- (void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
    NSLog(@"Left Image clicked");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文