ios - 将 gestureRecognizer 添加到 tableView 单元格

标签 ios swift xcode uitableview uigesturerecognizer

我目前有一个包含 3 个单元格的 TableViewController,我正在尝试添加一个长按手势识别器,以便在检测到时仅打印到日志中。

我添加了:

class TableTesting: UITableViewController, UIGestureRecognizerDelegate 

在我的 tableView 方法中,我创建了一个 UILongPressGestureRecognizer:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "Gesture Recognizer Testing"
    var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressAction:")
    lpgr.minimumPressDuration = 2.0
    lpgr.delegate = self
    cell.addGestureRecognizer(lpgr)
    return cell
}

我还创建了函数 longPressAction:

func longPressAction(gestureRecognizer: UILongPressGestureRecognizer) {
    print("Gesture recognized")
}

我遇到的问题是在编译代码并尝试长按我的单元格时,应用程序崩溃并且出现此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestingGround.TableTesting longPressAction:]: unrecognized selector sent to instance 0x7f9afbc055d0'

我以某种方式猜测正确的信息没有传递到函数中,但我不确定?

如有任何帮助,我们将不胜感激。

最佳答案

代替:

var lpgr = UILongPressGestureRecognizer(target: self, action: "longPressAction:")

使用:

let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(gestureRecognizer:)))

关于ios - 将 gestureRecognizer 添加到 tableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39963708/

相关文章:

iOS 如何将字符串 Class 1 发送到 Class 2

ios - 不存在 CFBundleIdentifier 为 'xxxxxx' 的软件。

swift 3 : How to do string ranges?

ios - xcode 6 webview 全屏

ios - 如何在xcconfig文件中配置完整URL

ios - UITableView commitEditingStyle 仅适用于特定情况

ios - 如何理解保留计数

swift - PDFKit注释保存文本注释

swift - AVCaptureDevice.requestAccessForMediaType 从不弹出窗口,始终返回 false

ios - 无法在我的 firstViewController 中使用类型为“(String)”的参数列表调用 'append'