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

将数据从tableView传递到Swift中的ViewController,第1张

概述我有一个应用程序,我正在尝试完全适应我想要的 我一直在关注Seemu Apps的Youtube教程,但是我需要添加一个可选的ViewController来完成它 这个应用程序有2个tableViews显示车辆,如果我们点击第一个tableView的一行,然后第二个tableView将显示所选车辆的列表. 这是我们到现在为止所拥有的:(图片链接,因为我在stackOverFlow上没有得到十分的声誉 我有一个应用程序,我正在尝试完全适应我想要的

我一直在关注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.swiftimport UIKitclass 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 UIKitclass 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 UIKitclass 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 UIKitclass 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)    }

这应该工作.

总结

以上是内存溢出为你收集整理的将数据从tableView传递到Swift中的ViewController全部内容,希望文章能够帮你解决将数据从tableView传递到Swift中的ViewController所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048360.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存