Passing data from a View table to a ViewController in Swift

I have an application that I am trying to configure exactly as I want

I am following the Seemu Apps Youtube tutorial to do this, but I need to finish it by adding an additional ViewController

There are 2 tables showing vehicles in this application, and if we click on one row of the first table, then the second tableView will show us a list of the selected vehicles.

Here is what we have so far: (link to the image, because I do not have a reputation for ten points in stackOverFlow)

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

Everything works fine, but I want to be able to display information in an additional detailViewController (a shortcut with a detailed description of each car and a larger image of it) depending on which car we click in the secondTableViewControlle (or modelViewController in the application) exactly as I followed in the tutorial between tableViews

I know that we need to transfer data using the prepareForSegue method, I figured this out by following the steps in the tutorial, but when we have 2 tableviewControllers

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

What do we need to do to show information about each car?

PD: I'm new to the programming world, maybe I will need to see this in a very simple way.

All 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. } 
+5
source share
1 answer

Try it.

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) } 

That should work.

+7
source

Source: https://habr.com/ru/post/1211534/


All Articles