从 cellForItemsInSection 中的 #selector 调用带有两个参数的函数
Call a function with two parameters from #selector in cellForItemsInSection
这是我的功能:
func myFunction(sender:UILongPressGestureRecognizer, index: Int){
print(sender.view?.tag)
}
在 cellForItemsInSection 中,这是我所做的:
let gesture UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(self.myFunction(_:))) //here I want to call the second parameter with the indexPath.row but how?
gesture.view?.tag = indexPath.row
像这样:
let gesture = UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(myFunction(sender:index:)))
如果您在 selector
前加上 class,您可以获得代码补全。
所以如果你有 MyViewController
和 myFunction
,你可以写
gesture.addTarget(self, action: #selector(MyViewController.))
并且代码完成应该可以从那里帮助你,正如你在这张图片中看到的那样(是的,我在一个名为 CreateUserViewController
的 class 中,但忽略它 :))
希望对你有所帮助。
编辑
@vadian 是对的。我会留下这个答案,这样你就可以看到如何使用代码完成,但我的答案不正确,vadians 是。
您不能在一个 UIGestureRecognizer
操作中传递两个参数。
The action methods invoked must conform to one of the following
signatures:
- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
您不能像@vadian 所说的那样在 UIGestureRecognizer 操作中传递两个参数。
您只需执行此操作即可在您的函数中获得 indexPath.row
let gesture = UILongPressGestureRecognizer()
gesture.view?.tag = indexPath.row
gesture.addTarget(self, action: #selector(self.myFunction(_:)))
在你的myFunction
func myFunction(sender:UILongPressGestureRecognizer){
let tag = sender.view?.tag
//do stuff
}
这是我的功能:
func myFunction(sender:UILongPressGestureRecognizer, index: Int){
print(sender.view?.tag)
}
在 cellForItemsInSection 中,这是我所做的:
let gesture UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(self.myFunction(_:))) //here I want to call the second parameter with the indexPath.row but how?
gesture.view?.tag = indexPath.row
像这样:
let gesture = UILongPressGestureRecognizer()
gesture.addTarget(self, action: #selector(myFunction(sender:index:)))
如果您在 selector
前加上 class,您可以获得代码补全。
所以如果你有 MyViewController
和 myFunction
,你可以写
gesture.addTarget(self, action: #selector(MyViewController.))
并且代码完成应该可以从那里帮助你,正如你在这张图片中看到的那样(是的,我在一个名为 CreateUserViewController
的 class 中,但忽略它 :))
希望对你有所帮助。
编辑
@vadian 是对的。我会留下这个答案,这样你就可以看到如何使用代码完成,但我的答案不正确,vadians 是。
您不能在一个 UIGestureRecognizer
操作中传递两个参数。
The action methods invoked must conform to one of the following signatures:
- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
您不能像@vadian 所说的那样在 UIGestureRecognizer 操作中传递两个参数。
您只需执行此操作即可在您的函数中获得 indexPath.row
let gesture = UILongPressGestureRecognizer()
gesture.view?.tag = indexPath.row
gesture.addTarget(self, action: #selector(self.myFunction(_:)))
在你的myFunction
func myFunction(sender:UILongPressGestureRecognizer){
let tag = sender.view?.tag
//do stuff
}