Reputation: 5767
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?
@interface MagTableViewController : UITableViewController <UIGestureRecognizerDelegate>
@property (strong, nonatomic) UILongPressGestureRecognizer *longPress;
@property (strong, nonatomic) NSMutableArray *tableContent;
@end
- (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
}
Upvotes: 1
Views: 293
Reputation: 1827
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
}
Upvotes: 1
Reputation: 7416
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?
Upvotes: 1