ios 在表格视图单元格上长按手势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9367898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:55:29  来源:igfitidea点击:

long press gesture on table view cell

objective-ciosuitableviewuigesturerecognizer

提问by user1120008

I want two interactions on a table view cell: normal tap and long press. I used the answer to the following to help me get started:

我想要在表格视图单元格上进行两次交互:正常点击和长按。我使用以下答案来帮助我入门:

Long press on UITableView

长按 UITableView

The problem with that is if I do a long press on a valid cell, the cell will highlight blue, and the long press gesture does not fire (it thinks its just a simple tap). However, if I start the long press gesture on a non-valid cell, then slide my finger over to a valid cell then release, it works just fine.

问题是如果我长按一个有效的单元格,该单元格将突出显示蓝色,并且长按手势不会触发(它认为它只是一个简单的点击)。但是,如果我在无效的单元格上开始长按手势,然后将手指滑到有效的单元格上然后松开,它就可以正常工作。

采纳答案by Teofilo Israel Vizcaino Rodrig

Maybe disabling selection in IB or programatically

也许禁用 IB 中的选择或以编程方式

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

回答by Prof Von Lemongargle

There is probably a better answer out there, but here is one way to do it:

那里可能有更好的答案,但这是一种方法:

First create a long press gesture recognizer on the table view itself.

首先在表格视图本身上创建一个长按手势识别器。

UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
[self.tableView addGestureRecognizer:longPressRecognizer];

Then, handle it with a routine that can find the selected row:

然后,使用可以找到所选行的例程来处理它:

-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture
{
if (pGesture.state == UIGestureRecognizerStateRecognized)
{
    //Do something to tell the user!
}
if (pGesture.state == UIGestureRecognizerStateEnded)
{
    UITableView* tableView = (UITableView*)self.view;
    CGPoint touchPoint = [pGesture locationInView:self.view];
    NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint];
    if (row != nil) {
        //Handle the long press on row
    }
}
}

I haven't tried it, but I think you could keep the row from showing selection by making the gesture recognizers on the table view wait for the long press to fail.

我还没有尝试过,但我认为您可以通过让表格视图上的手势识别器等待长按失败来防止行显示选择。

回答by Fabio Berger

I came across the same problem and found a good solution. (at least on iOS 7)

我遇到了同样的问题,并找到了一个很好的解决方案。(至少在 iOS 7 上)

Add this UILongPressGestureRecognizerto the cell itself.

将此添加UILongPressGestureRecognizer到单元格本身。

self.longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onSelfLongpressDetected:)];
[self addGestureRecognizer:self.longPressGesture];

Its weird but important to init with the target to self, AND also add the gestureRecognizer again to self and the method onSelfLongpressDetectedgets called.

将目标初始化为 self 很奇怪但很重要,并且还将gestureRecognizer 再次添加到self 并onSelfLongpressDetected调用该方法。

回答by thacilima

I had a problem close to this. First I tried to add a long press gesture to an UIView inside a selectable cell and it didn't work. The solution was to add the gesture to the cell itself, like it was said before in Fabio's answer.

我有一个接近于此的问题。首先,我尝试向可选单元格内的 UIView 添加长按手势,但没有成功。解决方案是将手势添加到单元格本身,就像之前 Fabio 的回答中所说的那样。

Adding the solution in swift bellow:

在 swift bellow 中添加解决方案:

let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPress(_:)))
longPress.minimumPressDuration = 1.0
cell.addGestureRecognizer(longPress)

I used the code above inside the UITableViewDataSource method cellForRowAtIndexPath.

我在 UITableViewDataSource 方法 cellForRowAtIndexPath 中使用了上面的代码。