我想开发自己的应用程序,为了做到这一点,我需要能够传递一些信息,取决于用户点击哪一行(它是 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() }
问题是:
如果用户点击法拉利458,DetailViewController中的lblDetail将显示:法拉利458是超级跑车,能够达到325公里/小时……(无论我们想要什么)
和imgDetail将能够显示一个图像(无论我们想要什么)的汽车
如果用户点击布加迪威龙现在,lblDetail显示:布加迪威龙是一款完美超级跑步机.它是世界上最快的车之一….
imgDetail向我们展示了这辆车的形象
与所有汽车相同的事情,根据我们点击了哪一行
我知道工作是在第一个View Controller中的prepareForSegue func中,但是我正在尝试许多不同的方法来使其成为可能,任何运行正常
我们该怎么办?
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中创建一个已传递的值.
这只是将数据从一个viewController传递给另一个的示例,您可以根据需要传递此示例的数据.
有关更多信息,请参阅此链接.
Passing values between ViewControllers based on list selection in Swift
Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?
Swift: Pass UITableViewCell label to new 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 } }