【发布时间】:2019-11-17 17:40:06
【问题描述】:
我想将longPressGestureRecocnizer 添加到集合视图单元格并传入单元格的 indexPath 以使用它。我尝试通过将其添加到 cellForItemAt 方法来做到这一点:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressGetstureFunction(indexPath:)))
cell.addGestureRecognizer(longPressGesture)
这是我用#Selector 调用的方法:
@objc func longPressGetstureFunction(indexPath: Int) {
print("long press gesture detected")
Alert.showColectionViewDataAlert(on: self, indexRow: indexPath)
}
但是当我像这样为 indexPath 传入一个整数时:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressGetstureFunction(indexPath: 5)))
我从 Xcode 收到以下错误消息:
Argument of '#selector' does not refer to an '@objc' method, property, or initializer
我在这个主题上搜索了很多,我还在 StackOverflow 上看到了一些同类问题的答案,但所有答案要么有 Objective-C 代码,要么有与 colectionView 无关的代码。
有人知道我会怎么做吗?
谢谢! 本吉
【问题讨论】:
-
这能回答你的问题吗? Passing arguments to selector in Swift
-
@bsod 我在谷歌上看到了这个答案,但它没有回答我的问题
标签: swift xcode uicollectionview uicollectionviewcell