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
Paul Brazell
14,371 PointsUnable to get count of items from JSON before setting up Table View Cells
Hey guys. I am having a bit of trouble figuring this out. I am pulling data from a JSON request. I am able to parse it appropriately, but when I am trying to lay out the number of table view cells to create based on the JSON dictionary, I am getting returned 0 entries. I have a few records in this database so far and I can print it during the closure, but Im just not sure where I am going wrong. I assumed the viewDidLoad method would be the best place for this, but i guess not ha. Anyways, here is my code.
class MasterTableViewController: UITableViewController {
var customersDictionary: [Dictionary<String, AnyObject>] = []
override func viewDidLoad() {
super.viewDidLoad()
// data
let urlPath: String = "myPHPLocation" //this will be changed to the path where service.php lives
let url: NSURL = NSURL(string: urlPath)!
// USE NSURLSession API to fetch data
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration)
// NSURL Request object
let request = NSURLRequest(URL: url)
let dataTask = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let customers = json["Customers"] as? [[String: AnyObject]] {
self.customersDictionary = customers
print(self.customersDictionary.count)
}
} catch {
print("error serializing JSON: \(error)")
}
}
dataTask.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return customersDictionary.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Get references to labels of cell
cell.textLabel?.text = "Test"
// Configure the cell...
return cell
}
Any help would be greatly appreciated. Thank you in advance.
1 Answer
Paul Brazell
14,371 PointsSolved it. needed to do a tableView.reloadData() after the viewDidLoad