rushafi
rushafi

Reputation: 861

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?

Upvotes: 1

Views: 6613

Answers (3)

Rad'Val
Rad'Val

Reputation: 9231

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

}

Upvotes: 4

Ariel
Ariel

Reputation: 2440

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.

Upvotes: 1

Dancreek
Dancreek

Reputation: 9544

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

Upvotes: 1

Related Questions