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

前端之家收集整理的这篇文章主要介绍了将数据从tableView传递到Swift中的ViewController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,我正在尝试完全适应我想要的

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

这个应用程序有2个tableViews显示车辆,如果我们点击第一个tableView的一行,然后第二个tableView将显示所选车辆的列表.

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

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

一切都运行完美,但我希望能够在可选的detailViewController中显示信息(标签上有每个车辆的详细描述和更大的图像),具体取决于我们在secondTableViewControlle(或应用程序中的modelViewController)中单击的车辆究竟我是如何在tableViews之间的教程中关注的

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

例如:如果我们想要显示最后一个viewController,其中包含法拉利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,UITableViewDataSource {

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

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

        //Register custom cell

        var nib = UINib(nibName: "customCell2",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: 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.
    }
尝试这个.

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

这应该工作.

原文链接:https://www.f2er.com/swift/319998.html

猜你在找的Swift相关文章