1

I have a UITableView with UITableViewCell which holds UIImageView's. Now I want to add a UILongGestureRecognizer to the UIImageView's. But this does not work. The UILongGestureRecognizer works on self.view...

How to implement the UILongGestureRecognizer that it works on the UIImageView's in the UITableViewCell's?

TableViewController.h

@interface MagTableViewController : UITableViewController <UIGestureRecognizerDelegate>

@property (strong, nonatomic) UILongPressGestureRecognizer *longPress;
@property (strong, nonatomic) NSMutableArray *tableContent;

@end

TableViewController.m

- (void)viewDidLoad
{
    self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    self.longPress.minimumPressDuration = 0.2;
    self.longPress.numberOfTouchesRequired = 1;
    //[self.view addGestureRecognizer:self.longPress];  // This works! 
}

// [...]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIImageView *imvLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [imvLeft setImageWithURL:[NSURL URLWithString:self.tableContent[@"url"]]];
    imvLeft.userInteractionEnabled = YES; // added soryngod's hint, but does not
    // solve the problem, as only the last row of 5 is enabled...
    [imvLeft addGestureRecognizer:self.longPress];  // does not work... 

    [cell.contentView addSubview:imvLeft];

    return cell;
}

-(void)longPressed:(UILongPressGestureRecognizer *)recognizer {
// do stuff

}
jerik
  • 5,714
  • 8
  • 41
  • 80

2 Answers2

1

You need to set imvLeft.userInteractionEnabled = YES; by default it is NO.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
        longPress.minimumPressDuration = 0.2;
        longPress.numberOfTouchesRequired = 1;
        imvLeft.userInteractionEnabled = YES;
        [imvLeft addGestureRecognizer:self.longPress];
        [longPress release];
    [cell.contentView addSubview:imvLeft];

    return cell;
}

And if you want to identify the imageview that was pressed

-(void)longPressed:(UILongPressGestureRecognizer *)recognizer 
{

 UIImageView *img = (UIImageView *)recognizer.view;

//do stuff
}
soryngod
  • 1,827
  • 1
  • 14
  • 13
  • Somehow this just works on the last imageView (row) in a section. I have 5 rows in a section, all based on the same code. When a new section appears, the last imageView (row) of the previous section does not work anymore, just the new one... – jerik Jul 16 '13 at 18:26
1

In addition to setting imvLeft.userInteractionEnabled = YES, you also need to make a distinct gesture recognizer for each image view. By design, UIGestureRecognizer must be associated with a single view. The symptoms you are seeing are a result of the recognizer being unattached from the previous cell as each new one calls addGestureRecognizer:.

See related question: Can you attach a UIGestureRecognizer to multiple views?

Community
  • 1
  • 1
jszumski
  • 7,430
  • 11
  • 40
  • 53