如何检测UITableViewCell内部的触摸

时间:2022-11-08 00:18:54

I have a custom UITableViewCell with four UIImageView inside it. I want to detect touch on these imageViews and notify my ViewController (which is containing the UITableView) which ImageView inside which cell has been tapped. How may I do that?

我有一个自定义的UITableViewCell,里面有四个UIImageView。我想检测这些imageViews上的触摸并通知我的ViewController(它包含UITableView)哪个CellView在哪个单元格中被点击。我该怎么办?

3 个解决方案

#1


1  

There are a couple variants to hit the target. In fact it doesn't matter at all how you are doing it at the cell's level. It may be buttons, image view with tap gesture recognizers or custom drawing and proceeding touch events. I will not provide you with code now as you have a lot of code already given and may find a lot more by little search, thought it may be provided by demand. The real "problem" is in transporting message to the controller. For that purpose I've found only two reasonable solutions. First is the notifications and second is the delegations. Note that the notifications method may lead to visible lag between tap and actual event occurrence as sometimes notifications are brought to objects with little delay...
Hope that it helps.

有几种变种可以达到目标。事实上,你在细胞水平上如何做这件事并不重要。它可以是按钮,具有敲击手势识别器的图像视图或自定义绘图和继续触摸事件。我现在不会为您提供代码,因为您已经提供了大量代码,并且可能会通过少量搜索找到更多代码,认为它可能是由需求提供的。真正的“问题”是将消息传送到控制器。为此,我发现只有两个合理的解决方案。首先是通知,第二是代表团。请注意,通知方法可能导致点击和实际事件发生之间的可见滞后,因为有时通知会被带到没有延迟的对象...希望它有所帮助。

#2


4  

Try this:

-(void)createGestureRecognizers {

  for(/*each of the UIImageViews*/) {
      UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]

      initWithTarget:self action:@selector(handleSingleTap:)];

      singleFingerTap.numberOfTapsRequired = 1;

      [imageView addGestureRecognizer:singleFingerTap];

      imageView.tag = i;

      [singleFingerTap release];
  }

}


- (void)handleSingleTap:(UIGestureRecognizer *)sender {

    UIImageView *imageView = (UIImageView *)sender.view;//this should be your image view

}

#3


1  

Use four buttons and set them with different tag values. Then just check the tag value to see which one was pressed.

使用四个按钮并使用不同的标签值进行设置。然后只需检查标签值以查看按下了哪一个。

#1


1  

There are a couple variants to hit the target. In fact it doesn't matter at all how you are doing it at the cell's level. It may be buttons, image view with tap gesture recognizers or custom drawing and proceeding touch events. I will not provide you with code now as you have a lot of code already given and may find a lot more by little search, thought it may be provided by demand. The real "problem" is in transporting message to the controller. For that purpose I've found only two reasonable solutions. First is the notifications and second is the delegations. Note that the notifications method may lead to visible lag between tap and actual event occurrence as sometimes notifications are brought to objects with little delay...
Hope that it helps.

有几种变种可以达到目标。事实上,你在细胞水平上如何做这件事并不重要。它可以是按钮,具有敲击手势识别器的图像视图或自定义绘图和继续触摸事件。我现在不会为您提供代码,因为您已经提供了大量代码,并且可能会通过少量搜索找到更多代码,认为它可能是由需求提供的。真正的“问题”是将消息传送到控制器。为此,我发现只有两个合理的解决方案。首先是通知,第二是代表团。请注意,通知方法可能导致点击和实际事件发生之间的可见滞后,因为有时通知会被带到没有延迟的对象...希望它有所帮助。

#2


4  

Try this:

-(void)createGestureRecognizers {

  for(/*each of the UIImageViews*/) {
      UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]

      initWithTarget:self action:@selector(handleSingleTap:)];

      singleFingerTap.numberOfTapsRequired = 1;

      [imageView addGestureRecognizer:singleFingerTap];

      imageView.tag = i;

      [singleFingerTap release];
  }

}


- (void)handleSingleTap:(UIGestureRecognizer *)sender {

    UIImageView *imageView = (UIImageView *)sender.view;//this should be your image view

}

#3


1  

Use four buttons and set them with different tag values. Then just check the tag value to see which one was pressed.

使用四个按钮并使用不同的标签值进行设置。然后只需检查标签值以查看按下了哪一个。