Rodrigo Schneider
Rodrigo Schneider

Reputation: 345

Send label text data with segue in TableView in Swift

Good evening.

I have here in Swift a TableView that lists some items. I want to, when the user click on it, show a new ViewController with details about that item (like name, photo, desc.) So, here is my tableView funcs:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return listExample.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cellExample", forIndexPath: indexPath) as! ExampleTableViewCell

    //Remove separators margins
    cell.layoutMargins = UIEdgeInsetsZero

    cell.labelExample.text = listExample[indexPath.row]

    return cell
}

I've done some research and found that maybe I can do that with that function:

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

But, how can I send the selected item (the label in this case) to the new viewController where I'll have an other label for title, for example?

Upvotes: 1

Views: 1581

Answers (3)

Rodrigo Schneider
Rodrigo Schneider

Reputation: 345

I've finally done that using this example: Passing data between two UIViewControllers using Swift and Storyboard

It's make a lot easier and helps me finally get it to work!

Thanks!

Upvotes: 0

yesthisisjoe
yesthisisjoe

Reputation: 2035

First create a segue from your View Controller with the Table View in it to the second View Controller. Give the segue an identifier, such as "detailViewSegue".

You have the right idea with didSelectRowAtIndexPath. Add a line to it so it looks like this (with the corresponding segue identifier):

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("detailViewSegue", sender: self)
}

Finally override the function prepareForSegue. You will need a class variable in your second View Controller to pass the data to. In this example dataToPass in the first View Controller is passed to dataPassed in the second.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "detailViewSegue") {
        //replace SecondViewController with the name of your second view controller
        let vc = segue.destinationViewController as! SecondViewController

        vc.dataPassed = self.dataToPass
        vc.delegate = self
    }
}

Upvotes: 2

vien vu
vien vu

Reputation: 4337

You can do like this in this function:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let text = listExample[indexPath.row]
    self.performSegueWithIdentifier(yourSegueIdentifier, sender: text)
}

And you override this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == yourIdentifierAbove {
        let controller = segue.destinationViewController as! YourControllerType
        let textPass = sender as! String

        controller.variableWillReceive = textPass
    }

}

Upvotes: 2

Related Questions