将数据从TableView发送到DetailView Swift

我试图做也许最简单和更混乱的事情之一,直到现在我想开发我自己的应用程序,为了做到这一点,我需要能够传递一些信息,取决于哪个行用户点击(这是Swift lenguage)

我们有一个RootViewController(表视图)和一个DetailViewController(有1个标签和1个图像) 在这里输入图像描述

(我们的观点)

这里是代码:

@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文件)

 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公里/小时的超级车……(无论我们想要什么),imgDetail都能够显示图像我们想要)的车

如果用户点击布加迪威龙现在lblDetail告诉我们:布加迪威龙是一个完美的超级运动机器。 这是世界上最快的汽车之一….

imgDetail向我们展示了这款车的形象

与所有车辆同样的事情取决于我们点击了哪一行

我知道工作是在第一视图控制器prepareForSegue函数周围,但我想了很多不同的方式使它成为可能,任何东西运行正常

我们如何做到这一点?

这里是你的例子:

 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创build一个passedValuevariables。

这只是将数据从一个viewController传递到另一个viewController的例子,你可以根据需要用这个例子传递数据。

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

根据Swift中的列表select在ViewControllers之间传递值

为UITableView使用didSelectRowAtIndexPath或prepareForSegue方法?

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

https://teamtreehouse.com/forum/help-swift-segue-with-variables-is-not-working

可能这会帮助你。

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

它很简单,我正在给上面的答案增加一个声明。 要详细查看所选车名的视图标签,

lblDetail.text = passedValue

你可以在你的详细视图的viewDidLoad()函数中添加这行代码。 passedValue包含用户select的车的名字(在prepareForSegue中赋值),那么你可以指定给你的detailedView标签。

希望它有帮助!

Interesting Posts