Lindsay
Lindsay

Reputation: 1

How to pass data from a tableview to a different uiviewcontroller

I am writing an application that is going to contain a tableview that contains a list of days. When a day is clicked, I would like to display a page that contains information with text and a button that is unique to each day.

I was planning on creating a different view controller that would be specific to each day. However, I do not know how to pass the data from the tableview for each day to the specific view controller of the specific day selected.

Upvotes: 0

Views: 191

Answers (3)

Anil Kumar
Anil Kumar

Reputation: 955

In your tableviewcontroller implement this code

class TableviewController: UITableViewController {

    var array : [DayObject]? = [DayObject(day: "Sunday", daytext: "SundayText"),DayObject(day: "Monday", daytext: "MondayText"),DayObject(day: "tuesday", daytext: "TuesdayText"),DayObject(day: "Wednesday", daytext: "WednesdayText")]
    var object: DayObject?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

extension TableviewController {


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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        cell?.textLabel?.text = array![indexPath.row].day
        return cell!
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        object =  array![indexPath.row]
        performSegueWithIdentifier("NVC", sender: self)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "NVC" {
            let dvc = segue.destinationViewController as? ViewController2
            dvc!.object = object
        }
    }

}

and make a datamodel as below:

import UIKit
class DayObject: NSObject {

    var day: String!
    var daytext: String!


    init(day: String, daytext: String) {
        self.day = day
        self.daytext =  daytext
    }
}

and in your view controller you can collect the object

class ViewController2: UIViewController {
    var object: DayObject!

    override func viewDidLoad() {
        super.viewDidLoad()
        print(object.daytext)
    }
}

By datamodel approach you dont have to make different view controllers for each day

happycoding :-)

Upvotes: 0

Rurouni
Rurouni

Reputation: 961

You can use UITableView delegate method for click event in your tableview You need to implement UITableViewDelegate. For passing data to specific view controller you may want to use prepareForSegue function

var day = [1,2,3,4,5]
var selected_day : Int = 0
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       self.day.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("yourcellidentifier") as! yourtableViewCell
        cell.labelday.text = self.day[indexPath.row]// just sample
        return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //this method will be called when you click 1 of the row from tableview
        self.selected_day = self.day[indexPath.row]
        self.performSegueWithIdentifier("ToYourSpecificViewController", sender: self) // you have to link with your table view controller and your specific view controller with an identifier.  
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         if segue.destinationViewController is YourSpecificViewController{
            let vc = segue.destinationViewController as! YourSpecificViewController
            // In YourSpecificViewController, you also need to declare a variable name called selected_day to catch 
            vc.selected_day = self.selected_day
         }
    }

Hope this help!

Upvotes: 3

Shades
Shades

Reputation: 5616

In the view controller with the table, implement the prepareforsegue() method:

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

    let row = self.tableView.indexPathForSelectedRow?.row

    if segue.identifier == "Sunday" {
        let vc = segue.destinationViewController as! SundayViewController
        vc.myInt = dataModel[row!].theInt  // This changes depending on how your data is set up and whether you're grabbing the info from a text field, or what have you
    }

    else if segue.identifier == "Monday" {   
        let vc = segue.destinationViewController as! MondayViewController
        vc.myInt = dataModel[row!].theInt
        vc.someString = dataModel[row!].theString
    }
}

The days' view controllers would look like:

class SundayViewController: UIViewController {

    var myInt: Int?
    // etc
}

class MondayViewController: UIViewController {

    var myInt: Int?
    var someString: String?
    // etc
}

Upvotes: 0

Related Questions