swiftsegue

How do I gain access to data from the selected cell in prepareForSegue


I'm attempting to create my first segue and I'd like to pass data from the tableViewCell that I selected to the secondary view controller that I'm segueing to.

My question is:

Since prepareForSegue doesn't take an indexPath, how do I gain information about the particular cell that I selected?

here's a photo of my app with some dummy data in it, the cells with arrows to the right represent the cells that I want to segue from:

enter image description here

This is my CustomCell class that those rows are derived from:

TimingCell.swift

class TimingCell: UITableViewCell {

    @IBOutlet weak var timingLabel: UILabel!
    @IBOutlet weak var timeSetLabel: UILabel!

    ...
}

here is where I am setting up my segue method:

ViewController.Swift

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, SettingCellDelegate {


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    var view: TimeViewController = segue.destinationViewController as! TimeViewController


}

*please let me know if you need to see anymore code in the comments below


Solution

  • So you should be segueing from the UITableViewController, not just the view controller.

    Inside the UITableViewController, you need to have a an override for:

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
    
        return 6 //Looks like you have six rows in your TableView
    }
    

    Now you need a class-scope NSIndexPath variable to keep track of the index of the row that was selected, so to create the variable:

    var path = NSIndexPath()
    

    Now you need two more overrides, the first to set the path variable when a row is selected, the second to deselect the row after you select it (ensures the deselect animation doesn't play when you transition back to the tableview from the child view controller):

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath : NSIndexPath) -> NSIndexPath? {
        path = indexPath
        return indexPath
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(path, animated: true)
    }
    

    Finally, in your prepareForSegue function, you may use the path variable to determine which row was selected and set the child view controller variables accordingly:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var dest = segue.destinationViewController as! TimeViewController
        if path == 1 {
            //set some destination variables here
        } else {
            //or here
        }
    }
    

    And if you have a data structure from which you're determining what each row in the table view will contain, just use path as an index to the data structure and pass that row's information to the destination view controller. For example, something like:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var dest = segue.destinationViewController as! TimeViewController
        dest.rowTitle = myRowsArray[path.row].title
    }
    

    Table views are a bit to get used to... good luck!