将UILongPressGestureRecognizer添加到自定义UITableViewCell内的UIButton

时间:2015-06-03 17:30:16

标签: ios objective-c uitableview uilongpressgesturerecogni

我有一个带有各种IBOutlets的自定义单元格,但在一个按钮上我想为长按手势添加一个UILongPressGestureRecognizer。这是我的代码(正确连接btw outlet并正确调用按钮的IBAction方法):

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");
        }
    }
}

问题是永远不会调用handleLongPressGestures:方法。

3 个答案:

答案 0 :(得分:1)

longPressGestureRecognizer应该是控制器上的属性而不是视图(MyCustomCell)。将属性移动到MyViewController,然后重试。当我将MyCustomCell排队并出列时,我的猜测是奇怪的。

要重用的对象(单元)应该是轻量级的。在这种情况下,longPressGestureRecognizer的目标是视图控制器并且很讨厌。

答案 1 :(得分:0)

试试这个!

-(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");
         }
      }
 }

答案 2 :(得分:0)

尝试这种方式

- (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];
    }
}

这种代码对我有用。