JoseMartinFit JoseMartinFit - 2 months ago 9
Swift Question

Call a function with two parameters from #selector in cellForItemsInSection

This is my function:

func myFunction(sender:UILongPressGestureRecognizer, index: Int){
print(sender.view?.tag)
}


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

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

  }
Comments