25

Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.

Question: How could I do it? Could any one show me the code, or any tutorial?

Thanks in advance!

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Anatoliy Gatt
  • 2,501
  • 3
  • 26
  • 42

5 Answers5

46

This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapMethod:)];

        [self.imageView addGestureRecognizer:tap];
        [self.imageView setUserInteractionEnabled:YES];
    }

    return self;
}

- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
    UIImageView *imageView = (UIImageView *)tapGesture.view;
    NSLog(@"%@", imageView);
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • In the myTapMethod, how would you access the imageView? – oyalhi Dec 21 '15 at 18:27
  • 1
    @oyalhi the UIGestureRecognizer class has a [view](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/index.html#//apple_ref/occ/instp/UIGestureRecognizer/view) property that will get you there. See my updated answer. – Mick MacCallum Dec 21 '15 at 19:39
6

Try this

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];

// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer 
{
    RKLogDebug(@"imaged tab");
}

make sure u have....

imageView.userInteractionEnabled = YES;
Raidri
  • 17,258
  • 9
  • 62
  • 65
WaaleedKhan
  • 685
  • 7
  • 18
  • + 1 for showing passing the recognizer into the function. Why this "tap.delegate = self;" when you are not actually using it? – Kevin Jan 06 '14 at 11:57
3

you can use a customButton instead UIImageView

iArezki
  • 1,273
  • 2
  • 17
  • 29
0

A tap gesture recogniser can be very memory hungry and can cause other things on the page to break. I would personally reconmend you create a custom table cell, insert a button into the custom cell frame and in the tableview code set the self.customtablecell.background.image to the image you want. This way you can assign the button an IBaction to make it push to whatever view you want.

Bradley
  • 617
  • 2
  • 11
  • 26
0

Use ALActionBlocks to action in block

__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
    NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];
dimohamdy
  • 2,917
  • 30
  • 28