UILongPressGestureRecognizer没有在第一个单元格上工作?

时间:2014-12-01 10:42:58

标签: ios objective-c

我尝试添加UILongPressGestureRecognizer以在按住单元格时显示警报视图,但由于某种原因它不能在表视图中的第一个单元格上运行。所以它适用于所有其他单元格,但索引path.row 0的单元格不起作用。有谁知道如何解决这个问题?

我的代码:

UIGestureRecognizerDelegate已添加到.h  UILongPressGestureRecognizer * longPress;在顶部实施;

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

    //gesture recognizer
    longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectCellHold)];
    longPress.delegate = self;
    [cell addGestureRecognizer:longPress];

    cell.lblComputer.text = [computer objectAtIndex:indexPath.row];
    cell.lblCustomer.text = [customer objectAtIndex:indexPath.row];
    return cell;
}

- (void)detectCellHold{
    CGPoint p = [longPress locationInView:self.tableView];
    NSIndexPath *indexOfHold = [self.tableView indexPathForRowAtPoint:p];
    if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"UIGestureRecognizerStateEnded");
        //Do Whatever You want on End of Gesture
    }
    else if (longPress.state == UIGestureRecognizerStateBegan){
        NSLog(@"%ld", (long)indexOfHold.row);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Computer" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Completed", @"Invoice", nil];
            [alert setTag:1];
            [alert show];

    }
}

更新: 它在我使用时有效:

- (void)detectCellHold{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Computer" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Completed", @"Invoice", nil];
                [alert setTag:1];
                [alert show];
            }

然而,这会多次显示警报?

2 个答案:

答案 0 :(得分:0)

添加识别器的好方法是,在滚动过程中,向已经有识别器的单元格添加新识别器,执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"activeComputers";
    ActiveComputersCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ActiveComputersCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        //gesture recognizer
        longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectCellHold)];
        longPress.delegate = self;
        [cell addGestureRecognizer:longPress];
    }   

    cell.lblComputer.text = [computer objectAtIndex:indexPath.row];
    cell.lblCustomer.text = [customer objectAtIndex:indexPath.row];
    return cell;
}

答案 1 :(得分:0)

跟进@ mityaika07的回答,

为什么在创建单元格时添加手势识别器?

您也可以将其添加到ActiveComputersCell

   UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectCellHold)];
   longPress.delegate = self;
   [self addGestureRecognizer:longPress];

这样,您可以在每次长按时自动获取特定的单元格实例。