0

I am pulling an array of object from parse.com using a query. I have NsLog statements that show that I am correctly retrieving data from parse but when I try and iterate over these objects and put information into an array that my tableview uses as it source, nothing shows up. Here is my code:

class FunListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet var tableView: UITableView!
var funlists = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    // Do any additional setup after loading the view, typically from a nib.

    var query = PFQuery(className: "FunLists")
    query.whereKey("createdBy", equalTo:"Sean Plott")
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in

        if error == nil {
            // The find succeeded.
            NSLog("Successfully retrieved \(objects.count) scores.")

            // Do something with the found objects
            for object in objects {
                NSLog("%@", object.objectId)
                self.funlists.append(object.objectId)
            }
        } else {
            // Log details of the failure
            NSLog("Error: %@ %@", error, error.userInfo!)
        }
    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  self.funlists.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    cell.textLabel!.text = self.funlists[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println("You selected cell #\(indexPath.row)!")
}

}

1 Answer 1

2

Once you are done updating the data source, you have to explicitly tell the table to reload.

Since data is provided inside a closure, and most likely in a different thread than the main, this is what you should add after the for loop:

    if error == nil {
        // The find succeeded.
        NSLog("Successfully retrieved \(objects.count) scores.")

        // Do something with the found objects
        for object in objects {
            NSLog("%@", object.objectId)
            self.funlists.append(object.objectId)
        }

        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
        }
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.