Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from tableView to ViewController in Swift

I have an App that i'm trying to adapt exactly how i want

I have been following a Youtube tutorial of Seemu Apps to make it but I need to finish it adding an optional ViewController

This app has 2 tableViews showing vehicles and if we click in one row of the first tableView then second tableView will show us a list of selected vehicles.

Here is what we have until now: (image link , because i haven't got ten points reputation on stackOverFlow)

http://subefotos.com/ver/?65ba467040cb9280e8ec49644fd156afo.jpg

All is running perfect, but i want to be able to display information in an optional detailViewController (label with a detailed description of each vehicle and a bigger image of this ) depending of which vehicle we click in the secondTableViewControlle (or modelViewController in the App) exactly how i was following in the tutorial between tableViews

i know that we need to passing data through prepareForSegue method , i have understood this making the steps in the tutorial but when we have 2 tableviewControllers

For example : if we want to display a last viewController with information of Ferrari 458 and a great picture of this car

What do we need to do exactly to show information of each vehicle?

PD : I'm beginner in the programming world, maybe i would need to see it in a very simple way

The whole code:

ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

     var selMake = String()

     @IBOutlet var tableView : UITableView!

    var transportData : [String] = ["Car", "Plane", "Motorcycle", "Truck" , "Train", "Bicycle" , "Helicopter"]

    //////////////////////////////////////////

    //viewDidLoad    
    override func viewDidLoad() {
        super.viewDidLoad()
        //Register custom cell

        var nib = UINib(nibName: "customCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")
    }

    //Numbers of rows in Section        
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.transportData.count
    }

    //cellForRowAtIndexPath        
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        ///// Static Cell (no valid for custom cells)

        /*
        var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = self.transportData[indexPath.row]

        return cell
        */

        var cell:customCellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as customCellTableViewCell

        cell.lblTrans.text = transportData[indexPath.row]
        cell.imgTrans.image = UIImage (named: transportData[indexPath.row])

        return cell
    }      

    //height        
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 90
    }

    //didSelectRowAtIndexPath        
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {            
        println("Fila \(transportData[indexPath.row]) seleccionada")            
        selMake = transportData[indexPath.row]            
        performSegueWithIdentifier("modelView", sender: self)
    }

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

        if(segue.identifier == "modelView") {                                
            var vc = segue.destinationViewController as modelViewViewController
            vc.selMake = selMake                            
        }            
    }

import UIKit

class customCellTableViewCell: UITableViewCell {
    @IBOutlet weak var imgTrans: UIImageView!
    @IBOutlet weak var lblTrans: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)    
        // Configure the view for the selected state
    }    
}

import UIKit

class modelViewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    //////////////////////////////////                
    var selMake = String()       
    var tableData : [String] = []
    @IBOutlet var tableView: UITableView!

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

        //Register custom cell

        var nib = UINib(nibName: "customCell2", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "cell")

        switch selMake {

        case "Car" :
            tableData = ["Ferrari 458", "La Ferrari"]

        case "Plane" :                
            tableData = ["Iberia"]

        case "Motorcycle" :                
            tableData = ["Kawasaki Ninja", "Yamaha Aerox"]

        case "Truck" :                
            tableData = [ "Camion transporte"]

        case "Train" :                
            tableData = [ "Ave" ]

        case "Bicycle" :                
            tableData = ["BMX"]   

        case "Helicopter" :                
            tableData = ["HelicopteroCombate"]

        default:
            println("Sel Make \(selMake)")                                                
        }            
        self.tableView.reloadData()                        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

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

       /* var cell : UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = self.tableData[indexPath.row]

        return cell*/

        var cell:customCell2TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as customCell2TableViewCell

        cell.lbl2text.text = self.tableData[indexPath.row]
        cell.img2image.image = UIImage (named: tableData[indexPath.row])

        return cell            
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println("Row \(indexPath.row)selected")

        performSegueWithIdentifier("detailView", sender: self)  
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 90
    }

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


        if(segue.identifier == "detailView") {
            var vc = segue.destinationViewController as DetailViewController 
        }      
    }

import UIKit

class customCell2TableViewCell: UITableViewCell {                
    @IBOutlet var lbl2text: UILabel!

    @IBOutlet var img2image: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }    
}

import UIKit    
class DetailViewController: UIViewController {                
    @IBOutlet var imgDetail: UIImageView!                
    @IBOutlet var lblDetail: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()   
        // Do any additional setup after loading the view.
    }
like image 394
FactorJose Avatar asked Jan 20 '15 17:01

FactorJose


1 Answers

Try this.

ModelViewViewController

var selectedImage:String?
var selectedLabel:String?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println("Row \(indexPath.row)selected")
        selectedImage = self.tableData[indexPath.row]
        selectedLabel = self.tableData[indexPath.row]
        performSegueWithIdentifier("detailView", sender: self)  
    }

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


        if(segue.identifier == "detailView") {
            var vc = segue.destinationViewController as DetailViewController 
           vc.img = selectedImage
           vc.lblDetail = selectedLabel
        }      
    }

class DetailViewController: UIViewController {                
    @IBOutlet var imgDetail: UIImage!                
    @IBOutlet var lblDetail: UILabel!
    var img:String?

override func viewDidLoad() {
        super.viewDidLoad()   
        // Do any additional setup after loading the view.

      imgDetail = UIImage(named: img)
    }

This should work.

like image 77
Diamond King Avatar answered Oct 23 '22 03:10

Diamond King