xcode Swift - 将手势识别器添加到表格单元格中的对象

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

Swift - add gesture recognizer to object in table cell

iosxcodeswiftuigesturerecognizer

提问by dan martin

I'm attempting to add a gesture recognizer to an object (an image, specifically) in a table view cell. Now, I'm familiar with gesture recognizers, but am left slightly confused as to how to set this up. The actual table cell doesn't have a viewDidLoad method, so I don't think I can declare the gesture recognizer there.

我正在尝试向表视图单元格中的对象(特别是图像)添加手势识别器。现在,我熟悉手势识别器,但对如何设置它有点困惑。实际的表格单元格没有 viewDidLoad 方法,所以我认为我不能在那里声明手势识别器。

This question (UIGestureRecognizer and UITableViewCell issue) seems to be related, however the answer is in objective C, and unfortunately I'm only fluent in swift.

这个问题(UIGestureRecognizer 和 UITableViewCell 问题)似乎是相关的,但是答案是在目标 C 中,不幸的是我只精通 swift。

If anybody could perhaps help me out on how I'd go about adding a gesture recognizer to an object in a table cell (NOT the whole tableview), or even perhaps aid me in translating the answer from the above link to swift, I'd be grateful

如果有人可以帮助我了解如何向表格单元格(不是整个表格视图)中的对象添加手势识别器,或者甚至可以帮助我将上述链接中的答案转换为 swift,我'感恩

回答by Dan Nichols

Here's a quick Swift-translation of the linked post's solution, adding the swipe gesture recognizer to the UITableView and then determining which cell the swipe happened on:

这是链接帖子解决方案的快速 Swift 翻译,将滑动手势识别器添加到 UITableView,然后确定滑动发生在哪个单元格上:

class MyViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var recognizer = UISwipeGestureRecognizer(target: self, action: "didSwipe")
        self.tableView.addGestureRecognizer(recognizer)
    }

    func didSwipe(recognizer: UIGestureRecognizer) {
        if recognizer.state == UIGestureRecognizerState.Ended {
            let swipeLocation = recognizer.locationInView(self.tableView)
            if let swipedIndexPath = tableView.indexPathForRowAtPoint(swipeLocation) {
                if let swipedCell = self.tableView.cellForRowAtIndexPath(swipedIndexPath) {
                    // Swipe happened. Do stuff!
                }
            }
        }
    }

}

回答by bpolat

here you go. Swift version of the solution you mentioned in your question

干得好。您在问题中提到的解决方案的 Swift 版本

"Instead of adding the gesture recognizer to the cell directly, you can add it to the tableview in viewDidLoad.

“您可以将手势识别器添加到 viewDidLoad 中的 tableview,而不是直接将手势识别器添加到单元格中。

In the didSwipe-Method you can determine the affected IndexPath and cell as follows:"

在 didSwipe-Method 中,您可以按如下方式确定受影响的 IndexPath 和单元格:”

func didSwipe(gestureRecognizer:UIGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.Ended {
        let swipeLocation = gestureRecognizer.locationInView(self.tableView)
          if let swipedIndexPath = self.tableView.indexPathForRowAtPoint(swipeLocation){
            if let swipedCell = self.tableView.cellForRowAtIndexPath(swipedIndexPath!){


      }
    }
  }
}

回答by David Seek

Update for Swift 4:

Swift 4 更新:

let swipeGestueRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(didRecognizeSwipeGestue(_:)))
self.view.addGestureRecognizer(swipeGestueRecognizer)

And the selector:

和选择器:

@objc func didRecognizeSwipeGestue(_ sender: UISwipeGestureRecognizer) {

    if sender.state == UIGestureRecognizerState.ended {
        let location = sender.location(in: self.tableView)
        if let indexPath = tableView.indexPathForRow(at: location) {
            if let cell = self.tableView.cellForRow(at: indexPath) {
                // todo
            }
        }
    }
}