简体   繁体   中英

How to detect touch inside an UITableViewCell

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?

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

}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM