Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 斯威夫特:长按手势识别器不起作用_Ios_Swift_Uitableview_Uilongpressgesturerecogni - Fatal编程技术网

Ios 斯威夫特:长按手势识别器不起作用

Ios 斯威夫特:长按手势识别器不起作用,ios,swift,uitableview,uilongpressgesturerecogni,Ios,Swift,Uitableview,Uilongpressgesturerecogni,我有一个uitable视图,我想为每一行添加uilongpressurerecognizer。 我尝试在表格单元格上拖动识别器并为其引用操作,但从未调用过 我也试过了 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: ident

我有一个
uitable视图
,我想为每一行添加
uilongpressurerecognizer
。 我尝试在表格单元格上拖动识别器并为其引用操作,但从未调用过

我也试过了

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ident, for: indexPath) as! TableViewCell
        /*...*/
    var longGesture = UILongPressGestureRecognizer(target: self, action: #selector(FilterPickerViewController.longPress))
    longGesture.minimumPressDuration = 1
    cell.leftLabel.addGestureRecognizer(longGesture)
    return cell
}

@objc func longPress(_ sender: UILongPressGestureRecognizer) {
    print("press")
}

但这也不起作用。我做错了什么?

您必须将长按手势识别器添加到表格视图中:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
  initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
[lpgr release];
然后在手势处理程序中:获取单元格索引:-

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.myTableView];

    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {
        NSLog(@"long press on table view but not on a row");
    } else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"long press on table view at row %ld", indexPath.row);
    } else {
        NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);
    }
}

泰,这很有效。你能告诉我[lpgr release]这一行需要什么,以及它是如何翻译成swift的吗?不过,如果没有它,它似乎可以工作