【问题标题】:Passing data from tableView to ViewController in Swift在 Swift 中将数据从 tableView 传递到 ViewController
【发布时间】:2015-03-19 01:15:26
【问题描述】:

我有一个应用程序,我正在尝试以我想要的方式进行调整

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

此应用有 2 个显示车辆的 tableView,如果我们单击第一个 tableView 的一行,那么第二个 tableView 将向我们显示所选车辆的列表。

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

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

一切运行完美,但我希望能够在可选的 detailViewController 中显示信息(带有每个车辆详细描述的标签和更大的图像),具体取决于我们在 secondTableViewControlle(或 modelViewController 中的应用程序)正是我在 tableViews 之间的教程中遵循的方式

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

例如:如果我们想显示最后一个带有法拉利 458 信息的 viewController 和这辆车的精美图片

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

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

【问题讨论】:

    标签: swift tableview


    【解决方案1】:

    试试这个。

    模型视图视图控制器

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

    这应该可行。

    【讨论】:

    • 在选择行之前先导航到新的 UIViewController 后为我工作,然后再跟踪数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多