一尘不染

在Swift中将数据从tableView传递到ViewController

swift

我有一个想要完全适应我想要的应用程序

我一直在关注Seemu Apps的Youtube教程,但是我需要添加一个可选的ViewController来完成它

该应用程序有2个显示车辆的tableViews,如果我们单击第一个tableView的一行,则第二个tableView将向我们显示选定车辆的列表。

这是到目前为止我们所拥有的:(图像链接,因为我在stackOverFlow上没有十分的声誉)

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

一切运行良好,但我希望能够在第二个TableViewControlle(或App中的modelViewController)中单击哪个车辆,以在可选的detailViewController(带有每辆车的详细说明的标签以及此车的更大图像)中显示信息。正是我在tableViews之间的教程中遵循的方式

我知道我们需要通过prepareForSegue方法传递数据,我已经理解了本教程中的步骤,但是当我们有2个tableviewControllers时

例如:如果我们要显示一个最后的viewController,其中包含Ferrari 458的信息以及此车的精美图片

我们到底需要做什么才能显示每辆车的信息?

PD:我是编程领域的初学者,也许我需要用一种非常简单的方式来了解它

整个代码:

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

阅读 401

收藏
2020-07-07

共1个答案

一尘不染

试试这个。

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

这应该工作。

2020-07-07