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 trialBrian Patterson
19,588 PointsPassing coordinates from a UITableView.
I know this might be out of the scope of this video. But I have an idea for an app that will have fixed locations in a TableView and I want to pass the coordinates into the following variable from the tableview let coordinate: (lat: Double, lon: Double) = (51.5072,-0.1275)
ignore the coordinates. How can this be done?
1 Answer
Gwinyai Nyatsoka
7,428 PointsIt will depend on the data source you used to display the coordinates on your TableView. Assuming you used an array as your data source, you can use the indexPath property of the TableView to know what the coordinates are for a particular row. Let's say each row's coordinates in your TableView was populated from this array
let coordinates[String: Double] = ["lat":51.00, "lon":45.78], ["lat":27.89,"lon":12.98]
If a user selects any particular row, you can get the coordinates from the TableView using the indexPath property of the TableView
if let indexPath = self.tableView.indexPathForSelectedRow() {
let selectedCoordinates = coordinates[indexPath.row]
}
You can now pass in these values into your variable
let coordinate: (lat: Double, lon: Double) = (selectedCoordinates["lat"] as! Double, selectedCoordinates["lon"] as! Double)