JoseMartinFit JoseMartinFit - 11 months ago 41
Swift Question

Call a function with two parameters from #selector in cellForItemsInSection

This is my function:

func myFunction(sender:UILongPressGestureRecognizer, index: Int){

In cellForItemsInSection this is what I do:

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

Answer Source

You can't pass two parameters in an UIGestureRecognizer action as @vadian said.

you can simply do this to get indexPath.row in your function

let gesture = UILongPressGestureRecognizer()
gesture.view?.tag = indexPath.row
gesture.addTarget(self, action: #selector(self.myFunction(_:)))

and in your myFunction

func myFunction(sender:UILongPressGestureRecognizer){
  let tag = sender.view?.tag

  //do stuff