将数据从TableView发送到DetailView Swift [英] Send data from TableView to DetailView Swift

查看:85
本文介绍了将数据从TableView发送到DetailView Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我正在尝试为我做一件最简单,更令人困惑的事情 我想开发自己的应用程序,为了做到这一点,我需要能够根据用户点击哪行来传递一些信息(这是Swift的语言)

I'm trying to do maybe one of the simplest and more confusing things for me until now I wanna develop my own App , and in order to do it I need to be able to passing some information depending of which row user click (it's Swift lenguage)

我们有一个RootViewController(表视图)和一个DetailViewController(带有1个标签和1个图像)

We have a RootViewController(table view) and a DetailViewController (with 1 label and 1 image)

(我们的视图)

这是代码:

@IBOutlet weak var tableView: UITableView!


var vehicleData : [String] = ["Ferrari 458" , "Lamborghini Murcielago" , "Bugatti Veyron", "Mercedes Benz Biome"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var nib = UINib(nibName: "TableViewCell", bundle: nil)

    tableView.registerNib(nib, forCellReuseIdentifier: "cell")



}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return vehicleData.count
}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell:TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as TableViewCell

    cell.lblCarName.text = vehicleData[indexPath.row]

    cell.imgCar.image = UIImage(named: vehicleData[indexPath.row])

    return cell   
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    performSegueWithIdentifier("DetailView", sender: self)
}

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "DetailView") {

        var vc = segue.destinationViewController as DetailViewController

    }

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 100
}

自定义TableViewCell类(具有带有单元格的xib文件)

Custom TableViewCell class (has a xib File with cell)

class TableViewCell: UITableViewCell {


@IBOutlet weak var lblCarName: UILabel!

@IBOutlet weak var imgCar: 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
}

class DetailViewController: UIViewController {



    @IBOutlet weak var lblDetail: UILabel!

    @IBOutlet weak var imgDetail: UIImageView!

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

问题是:

如果用户单击Ferrari 458,则DetailViewController中的lblDetail将显示:Ferrari 458是一款超级跑车,能够达到325 km/h的速度……(无论我们想要什么) 和imgDetail将能够显示汽车的图像(无论我们想要什么)

if user click Ferrari 458 , the lblDetail in DetailViewController would show: Ferrari 458 is a super car which is able to reach 325 km/ h ...... (whatever we want) and imgDetail would be able to show an image (whatever we want) of the car

如果用户单击布加迪威龙,则lblDetail向我们显示:布加迪威龙是一款完美的超级运动机.这是世界上最快的汽车之一....

If user click Bugatti Veyron now the lblDetail show us: Bugatti Veyron is a perfect and super sport machine. It's one of the fastest car in the world....

imgDetail向我们展示这辆车的图像

imgDetail show us an image of this car

所有汽车都有相同的东西,具体取决于我们点击了哪一行

Same thing with all cars depending which row we have clicked

我知道工作围绕着第一个View Controller中的prepareForSegue函数进行,但是我正在尝试许多不同的方法来使之成为可能,并且一切运行正常

I know the work is around prepareForSegue func in first View Controller but i was trying a lot of different ways to make it possible and anything runs ok

我们如何做到这一点?

推荐答案

以下是适合您的示例:

var valueToPass:String!

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

    valueToPass = currentCell.textLabel.text
    performSegueWithIdentifier("yourSegueIdentifer", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if (segue.identifier == "yourSegueIdentifer") {
        // initialize new view controller and cast it as your view controller
        var viewController = segue.destinationViewController as AnotherViewController
        // your new view controller should have property that will store passed value
        viewController.passedValue = valueToPass
    }
}

但是不要忘记在DetailViewController中创建一个passedValue变量.

But don't forget to create a passedValue variable into your DetailViewController.

这只是一个将数据从一个viewController传递到另一个viewController的示例,您可以根据需要使用此示例传递数据.

This is just an example of passing data from one viewController to another and you can pass data with this example as you need.

有关更多信息,请参阅此链接.

And for more info refer this links.

在ViewController之间基于列表中的选择传递值斯威夫特

使用didSelectRowAtIndexPath或UITableView的prepareForSegue方法吗?

Swift:将UITableViewCell标签传递给新的ViewController

https://teamtreehouse.com/forum/帮助变量快速搜索是无效的

也许这会对您有所帮助.

May be this will help you.

Swift 3.0

var valueToPass:String!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You selected cell #\(indexPath.row)!")

    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell

    valueToPass = currentCell.textLabel?.text
    performSegue(withIdentifier: "yourSegueIdentifer", sender: self)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if (segue.identifier == "yourSegueIdentifer") {
        // initialize new view controller and cast it as your view controller
        var viewController = segue.destination as! AnotherViewController
        // your new view controller should have property that will store passed value
        viewController.passedValue = valueToPass
    }
}

这篇关于将数据从TableView发送到DetailView Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆