I have a segue linked from a UITableViewCell to another UITableViewController within a navigation controller, and I need to setup the second UITableViewController based on the cell I'm segueing from.
The didSelectRowAtIndexPath method gives me that information but it gets called AFTER the prepareForSegue so I can't get the information I need in time.
I tried a couple of different things:
I've tried dragging a segue from another area on the first UITableViewController so I can just call the segue in code in the didSelectRowAtIndexPath method, but nothing seems to "stick".
There doesn't seem to be a way to just get the row and section from the UITableViewCell (it's the 'sender' in the prepareForSegue), unless I've missed something.
You can walk up the view hierarchy to find the containing table view cell and the containing table view. Then you can ask the table view for the cell's index path. Then use it to find the index path like this: func addButtonTapped(_ sender: Any) { guard let button = sender as?
In prepareForSegue:
, the table view's indexPathForSelectedRow
will be the path of the tapped row, if your segues are coming straight from the cells.
Or, you can use the table view's indexPathForCell:
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With