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 PointsTableView won't show at runtime.
I am adapting the current Weather App with a tableview of countries around the world. For some reason the tableview won't populate with the following code. Please can anyone help me. mport UIKit
class MasterTableViewController: UITableViewController {
var cities: [City]?
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
if cities?.count > 0 {
return
}
let london = City(name: "London", coordinates: (51.50722, -0.12750))
let melbourne = City(name: "Melbourne", coordinates: (-37.8136, 144.9631))
let singapore = City(name: "Singapore", coordinates: (-23.5475000,-46.6361100))
let saopaulo = City(name: "Sao Paulo", coordinates: (49.3297,8.57428))
let nurbergring = City(name: "Nurbergring", coordinates: (45.58005,9.27246))
cities?.append(london)
cities?.append(melbourne)
cities?.append(singapore)
cities?.append(saopaulo)
cities?.append(nurbergring)
tableView.reloadData()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let selectedCity = cities?[indexPath.row]
(segue.destinationViewController as! ViewController).city = selectedCity
}
}
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cities?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let city = cities?[indexPath.row]
cell.textLabel!.text = city!.name
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
}
Any help greatly appreciated!
1 Answer
Richard Lu
20,185 PointsThe variable
var cities: [City]?
was never initialized. This causes your method tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int to always return 0. You could rewrite it so that it comes out as:
var cities = [City]()
Let me know if this solves the problem :)
Brian Patterson
19,588 PointsBrian Patterson
19,588 PointsSorry for not getting back to you. I found the answer and it was to create an empty array. cities = []
This solved the issue.