Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
tromben98
13,273 PointsHow to display multiple properties from an object in a tableview cell to detail view controller using prepare for segue
Hi I am trying to display a name and a fact about an object in a detail view controller after a user pushes on the tableview cell. Grateful for any help I can get.
I think that it can have something to do with the controller.detailItem
I have the following arrays containing the object information:
let letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K, "L", "M"]
let numbers = ["1","2","3","4","5","6","7","8","9","10","11","12", "13"]
Prepare for segue metod:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let letters = letters[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = letters as AnyObject?
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
if let indexPath = self.tableView.indexPathForSelectedRow {
let numbers = numbers[indexPath.row]
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = numbers as AnyObject?
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
DetailViewController.swift:
@IBOutlet weak var letterLabel: UILabel!
@IBOutlet weak var numberLabel: UILabel!
ConfigureView method:
func configureView() { // Update the user interface for the detail item. if let letter = self.detailItem { if let label = self.letterLabel { label.text = letter.description
}
}
if let number = self.detailItem {
if let label = self.numberLabel {
label.text = number.description
}
}
}