flizana
flizana

Reputation: 569

Adding UILongPressGestureRecognizer to UIButton inside custom UITableViewCell

I have a custom cell with various IBOutlets, but on one button I want to add a UILongPressGestureRecognizer for long press gestures. Here is my code (btw outlets are connected correctly and the IBAction method of the button is called correctly):

MyCustomCell.h

@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer;
@end

MyCustomCell.m

- (void)awakeFromNib
{
    // Initialization code
    self.longPressGestureRecognizer = nil;
}

MyViewController.m

#import MyCustomCell.h

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell){
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
    cell.longPressGestureRecognizer.minimumPressDuration = 1.0f;
    cell.longPressGestureRecognizer.allowableMovement = 300.0f;
    [cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}

- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
    if ([recognizer.view isKindOfClass:[UIButton class]]){
        if (recognizer.state == UIGestureRecognizerStateBegan){
            NSLog(@"Long press began");
        } else if (recognizer.state = UIGestureRecognizerStateEnded){
            NSLog(@"Long press ended");
        }
    }
}

The problem is handleLongPressGestures: method is never called.

Upvotes: 1

Views: 704

Answers (3)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

Try This!

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILongPressGestureRecognizer *LongPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
    LongPress.minimumPressDuration = 1.0f;
    LongPress.allowableMovement = 300.0f;
    [cell.myButton addGestureRecognizer:LongPress];
}

- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
      if ([recognizer.view isKindOfClass:[UIButton class]]){
         if (recognizer.state == UIGestureRecognizerStateBegan){
             NSLog(@"Long press began");
         } 
         else if (recognizer.state = UIGestureRecognizerStateEnded)
         {
             NSLog(@"Long press ended");
         }
      }
 }

Upvotes: 0

Mrugesh Tank
Mrugesh Tank

Reputation: 3560

Try out this way

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell){
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if ([[cell gestureRecognizers] count]<1) {
        UILongPressGestureRecognizer *longPressGestureRecognizer;
        longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
        longPressGestureRecognizer.minimumPressDuration = 1.0f;
        longPressGestureRecognizer.allowableMovement = 300.0f;
        longPressGestureRecognizer.delegate = self;
        [cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
    }
}

This type of code works for me.

Upvotes: 0

lead_the_zeppelin
lead_the_zeppelin

Reputation: 2052

the longPressGestureRecognizer should be a property on the controller and not the view(MyCustomCell). Move the property over to MyViewController and try again. My guess is something weird is happening when it queues and dequeues the MyCustomCell.

Objects(cells) for reuse should be lightweight. In this case, the longPressGestureRecognizer's target is the view controller and is nasty.

Upvotes: 1

Related Questions