2015-06-03 3 views
1

У меня есть пользовательская ячейка с различными IBOutlets, но на одной кнопке я хочу добавить UILongPressGestureRecognizer для длинных жестов в прессе. Вот мой код (кстати выходы подключены правильно и IBAction метод кнопки называется правильно):Добавление UILongPressGestureRecognizer в UIButton внутри пользовательского UITableViewCell

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: метод никогда не вызывается.

ответ

1

longPressGestureRecognizer должно быть свойством на контроллере, а не видом (MyCustomCell). Переместите свойство в MyViewController и повторите попытку. Мое предположение - это что-то странное, когда он ставит в очередь и удаляет MyCustomCell.

Объекты (ячейки) для повторного использования должны быть легкими. В этом случае целью longPressGestureRecognizer является контроллер представления и является неприятным.

+0

Я также попытался использовать 'longPressGestureRecognizer' как свойство в контроллере представления, но не работал. – flizana

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

Этот тип кода работает для меня.

Смежные вопросы