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
Youseef Al Taher
Courses Plus Student 842 PointsRetrieving Data from Parse and displaying in TableView
Hey,
I am having trouble displaying the data that I retrieve from parse in the table view. If I use NSlog() or println(), I see the data retrieved in the console, so I know for a fact that I am receiving the data. I append the data retrieved to an array, and then I try to display this data on the tableview but it keeps coming up empty. If I hard code the data into the array, it gets displayed perfectly. (note: When i print the array in the console, I get the correct data in the array)
I have a feeling that tableview loads before the data is added to the array, hence the data is indeed retrieved, but not displayed. Can anyone help?
The code I have is written below:
import Foundation
import UIKit
class NameTableView: UITableViewController {
@IBOutlet var NameTableView: UITableView!
//Array of Wroker Names
var workerNames: [String] = []
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.workerNames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.NameTableView.dequeueReusableCellWithIdentifier("TransportCell") as UITableViewCell
cell.textLabel.text = self.workerNames[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
Parse.setApplicationId("-", clientKey: "-")
var query = PFQuery(className:"Carpenter")
query.whereKey("District", equalTo:"Hiliopoless")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// Looping through the objects to get the names of the workers in each object
for object in objects {
//Stringifying the name of the woker
var name_of_worker = object["Name"] as String
//Adding it to the array of Worker Names
self.workerNames.append(name_of_worker)
println(self.workerNames)
}
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
}
}
}
1 Answer
Stone Preston
42,016 Pointsyou need to reload the data in the tableView to "refresh" it after you get the data from the backend. this will force the delegate and dataSource methods to be called again, updating the tableView cells
// Looping through the objects to get the names of the workers in each object
for object in objects {
//Stringifying the name of the woker
var name_of_worker = object["Name"] as String
//Adding it to the array of Worker Names
self.workerNames.append(name_of_worker)
println(self.workerNames)
//reload the data
self.tableView.reloadData()
}
Youseef Al Taher
Courses Plus Student 842 PointsYouseef Al Taher
Courses Plus Student 842 PointsIt worked Perfectly, Thank you! there is a 2 second lag but whatever.
Stone Preston
42,016 PointsStone Preston
42,016 Pointsyes it takes time to make the network request so it wont be instant. you could add a spinning loading view if you wanted using a UIActivityIndicator