Michael St Clair Michael St Clair - 4 months ago 15x
iOS Question

Swift dynamic view based on cell tapped in previous table view

I have a table view with a list of events. I now am trying to create a dynamic view that can show details about all of those events based on the event tapped on. Two questions I have are how do I create a generic segue that I can use for any table cell to go to the same view, and then in that view how do I access the cell that brought the user to the view?


You would use didSelectRowAtIndexPath. First, create a variable eventToPass of type Event (or whatever your class is called) on your ViewController. Then, you should get the object that represents your data, then pass that to your new UIViewController. Something like:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let event = eventsArray[indexPath.row]
    self.eventToPass = event

    self.performSegueWithIdentifier("EventSegue", sender: self)

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "EventSegue") {
    var detailVC = segue.destinationViewController as EventDetailViewController
        detailVC.event = eventToPass 

This assumes that you have a ViewController called EventDetailViewController that has a property called event. From here, you can access any of the event's details on your new ViewController.