简体   繁体   English

如何在tableViewController中为自定义单元格按钮创建手势识别器,以识别其所在的单元格?

[英]How do you create a gesture recognizer for a custom cells button in a tableViewController that will be able to identify the cell it is in?

I have a custom cell which has a button that i am needing to recognize whether the user taps or does a long press. 我有一个自定义单元格,其中有一个按钮,我需要识别用户是否点击或长按。 I am able to recognize both but the long press gesture only works for the most recent cell made, while the cells prior do nothing when the button gets long pressed. 我能够识别两者,但长按手势仅适用于最近制作的单元格,而长按按钮时,先前的单元格不起作用。

//here is what i have in my cellForRowAtIndexPath //这是我的cellForRowAtIndexPath中的内容

self.longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
longPress.minimumPressDuration = 1.0f;
longPress.allowableMovement = 300.0f;

[cell.button addGestureRecognizer:longPress];

//testing LP //测试LP

  • (void)handleLongPressGestures:(UILongPressGestureRecognizer*)sender (void)handleLongPressGestures:(UILongPressGestureRecognizer *)sender

{ if([sender isEqual:self.longPress]){ {if([sender isEqual:self.longPress]){

    if(sender.state == UIGestureRecognizerStateBegan){
        [self performSegueWithIdentifier:@"changeValues" sender:self];
    }
}

} }

since it only works for the most recent cell made, i have also tried moving the initialization of the longPress properties to the view did load and assigning it to the button on creation of the cell but i still had the same results. 由于它仅适用于最新制作的单元格,因此我也尝试将longPress属性的初始化移至确实加载的视图,并在创建单元格时将其分配给按钮,但是我仍然得到相同的结果。 If anyone has any insight on doing something like this it would really be appreciated. 如果有人对做这样的事情有任何见识,将不胜感激。

This method worked for me and although it does not use gesture recognizer, it does do what i was wanting it to so i though it would post it incase someone else came across the same issue 这种方法对我有用,尽管它不使用手势识别器,但确实可以实现我想要的功能,因此,即使有人遇到相同问题,它也可以发布它

- (IBAction)numberAvailablePressed:(id)sender {

    pressedDown = NO;

}

- (IBAction)numberAvailableTouchedDown:(id)sender {
    pressedDown = YES;


    [NSTimer scheduledTimerWithTimeInterval:3.0
                                     target:self
                                   selector:@selector(checkIfPressed)
                                   userInfo:nil
                                    repeats:NO];

}

-(void)checkIfPressed
{
    if(pressedDown == YES)
    {
        [self performSegueWithIdentifier:@"changeValues" sender:self];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM