ios 如何在 Swift 中长按时选择表格行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30839275/
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-31 06:25:50  来源:igfitidea点击:

How To select a table row during a long press in Swift

iosswiftuigesturerecognizer

提问by LB79

I have a table which has a long press gesture recogniser that runs code depending on what table row is selected.

我有一个表,它有一个长按手势识别器,它根据选择的表行运行代码。

The trouble I'm having is that I currently have to tap the row I want then do the long press.

我遇到的麻烦是我目前必须点击我想要的行然后长按。

How can I make the table select the row that I am long pressing without having to tap to select it first?

如何让表格选择我长按的行而不必先点击选择它?

回答by user3687284

The following code works fine for me:

以下代码对我来说很好用:

Add a long press gesture recognizer in viewDidLoad:

在 viewDidLoad 中添加长按手势识别器:

// tapRecognizer, placed in viewDidLoad
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(longPressRecognizer)

Then the method invoke by the long press looks like this:

然后长按调用的方法如下所示:

//Called, when long press occurred
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {

    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {

        let touchPoint = longPressGestureRecognizer.locationInView(self.view)
        if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {

            // your code here, get the row for the indexPath or do whatever you want
    }
}

回答by PinkeshGjr

Swift 4

斯威夫特 4

override func viewDidLoad() {
    super.viewDidLoad()
    setupLongPressGesture()
}

func setupLongPressGesture() {
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self
    self.tblMessage.addGestureRecognizer(longPressGesture)
}

@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
    if gestureRecognizer.state == .began {
        let touchPoint = gestureRecognizer.location(in: self.tblMessage)
        if let indexPath = tblMessage.indexPathForRow(at: touchPoint) {

        }
    }
}

Swift 3

斯威夫特 3

override func viewDidLoad() {
    super.viewDidLoad()
    setupLongPressGesture()
}

func setupLongPressGesture() {
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self
    self.tblMessage.addGestureRecognizer(longPressGesture)
}

func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {

    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {

        let touchPoint = longPressGestureRecognizer.locationInView(self.view)
        if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {

            // your code here, get the row for the indexPath or do whatever you want
        }
    }
}

Objective - C

目标-C

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

-(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
        }
    }
}

回答by Kulker22

Swift 3 function:

斯威夫特 3 功能:

func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {

    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {

        let touchPoint = longPressGestureRecognizer.locationInView(self.view)
        if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) {

            // your code here, get the row for the indexPath or do whatever you want
    }
}

viewDidLoad:

视图加载:

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)

More: https://github.com/apple/swift-evolution/blob/e4328889a9643100177aef19f6f428855c5d0cf2/proposals/0046-first-label.md

更多:https: //github.com/apple/swift-evolution/blob/e4328889a9643100177aef19f6f428855c5d0cf2/proposals/0046-first-label.md

回答by Sazzad Hissain Khan

Swift 4

斯威夫特 4

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
self.view.addGestureRecognizer(longPressRecognizer)

// MARK: Actions

// 标记:动作

@objc func longPressed(sender: UILongPressGestureRecognizer) {

    if sender.state == UIGestureRecognizerState.began {

        let touchPoint = sender.location(in: self.tableView)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {

            print("Long pressed row: \(indexPath.row)")
        }
    }
}

回答by Pushpendra

For Verision 3 of swift

对于 swift 版本 3

func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
    if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
        let touchPoint = longPressGestureRecognizer.location(in: self.view)
        if let indexPath = notificationTabelView.indexPathForRow(at: touchPoint) {
            print("indexPath=\(indexPath)")
            // your code here, get the row for the indexPath or do whatever you want
        }
    }
}

In Your viewDidLoadfunction

在你的viewDidLoad职能中

let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(EmployeeNotificationViewController.longPress(_:)))
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self as? UIGestureRecognizerDelegate
    self.notificationTabelView.addGestureRecognizer(longPressGesture)

回答by Shamsudheen TK

To avoid this, you can add the UILongPressGestureRecognizerinside the cellForRowAtIndexPathinstead of didSelectRowAtIndexPath

为避免这种情况,您可以添加UILongPressGestureRecognizer内部cellForRowAtIndexPath而不是didSelectRowAtIndexPath

回答by Javier Vazquez

Using IBAction you can do (for a CollectionView):

使用 IBAction,您可以执行以下操作(对于 CollectionView):

@IBAction func handleLongPress(sender: AnyObject) {

    if sender.state == UIGestureRecognizerState.Began
    {
        let position = sender.locationInView(sender.view)

        if let indexPath : NSIndexPath = ((sender.view as! UICollectionView).indexPathForItemAtPoint(position))!{
            print("You holding cell #\(indexPath.item)!")
        }
    }
}

Remember to link with your Long Press Gesture Recognizer.

请记住与您的长按手势识别器链接。

回答by Chandramani

let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap)))
self.addGestureRecognizer(longPressGesture)

func longTap(){
    print("Long tap")
}