1

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.

flizana
  • 569
  • 6
  • 26

3 Answers3

1

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.

lead_the_zeppelin
  • 2,017
  • 13
  • 23
  • I have also tried using the `longPressGestureRecognizer` as a property in the view controller but didn't work. – flizana Jun 05 '15 at 17:39
0

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");
         }
      }
 }
Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59
Naresh Reddy M
  • 1,096
  • 1
  • 10
  • 27
0

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.

Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59