I have a tableview with a variable number of cells representing students that correspond to their particular instructor. They are custom cells with a button that triggers a segue to a new VC, bringing up detailed information on the student whose cell it was. My question is:
What is the best practice in swift for identifying which button was pressed?
Once i know the index path, I can identify which student's information needs to be passed to the next VC. There is a great answer for objective C in the post below, but I'm not sure how to translate to Swift. Any help would be much appreciated.
Best Answer
If your code allows, I'd recommend you set the
UIButton
tag equal to theindexPath.row
, so when its action is triggered, you can pull the tag and thus row out of the button data during the triggered method. For example, incellForRowAtIndexPath
you can set the tag:then in
buttonClicked:
, you can fetch the tag and thus the row:Otherwise, if that isn't conducive to your code for some reason, the Swift translation of this Objective-C answer you linked to:
is: