http://mobileapps1819.svctestudents.org/materials/TableViewFruit.zip
We did this project in the PM class but we did not have time in the AM class. This project shows how to pass information from a Table View cell selection to another view controller using a segue. This code passes the cell label text to the destination view controller. The code looks like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
let dest = segue.destination as? ViewController
let cell = sender as? UITableViewCell
// Pass the selected object to the new view controller.
dest?.newFruitText = (cell?.textLabel?.text)!
dest?.newFruitImage = (cell?.textLabel?.text?.lowercased())!
}
You may want to get the section and/or row number of the cell that got selected. You would want to do this if, for example, they clicked on the row 30 and you want to display president #30 or ant species #30 or HVAC symbol #30. This assumes you have a data file with presidents or ants or HVAC symbols (which you should have turned in for the data checkpoint).
Anyway, you can get the selected row using this somewhat roundabout way. Insert this into the above code where it says "pass the selected object" in place of or in addition to the code there.
dest.presidentNumber = self.tableView.indexPathForSelectedRow.row;
You can also get the section number from the indexPathForSelectedRow if your table view has sections for different kinds of data.
No comments:
Post a Comment