0

My UITableViewController, when initially viewed does not contain any data. However once, I go to another UIViewController, the data appears while the segue is still animating, and then the data is there. So to actually see the data, I need to go to another ViewController, and then back to the UITableViewController. I am using the Parse.com framework. The following code, is of the initial ViewController, which is also a UITableViewController that segues the the UITableViewController that does not contain any data initially. Thanks for the help, I am a newbie at IOS development, so please make answers in swift.
EDIT:Many of the answers or comments assume that the data is retrieved, and is just not loading unto the UITableViewController. However the problem is the fact that the data has not been retrieved by the time the segue has been completed. Thanks

override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject?){
    var cell = sender as UITableViewCell
    var text = cell.textLabel!.text
    currentScreen=text!
    println(currentScreen)
    groupConversation=[]
    var messageDisplayOne = PFQuery(className:currentScreen)
    messageDisplayOne.selectKeys(["userPost"])
    messageDisplayOne.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil{
            println("Type message \(groupConversation)")
            for object in objects {
                var textObject = object["userPost"] as String
                groupConversation.append(textObject)

            }
        } else {
            // Log details of the failure

        }
    }
}
11
  • try calling [tableview reloadData] in viewDidAppear. Commented Jan 2, 2015 at 16:24
  • @rakeshbs would that look like tableview.reloadData in swift? Commented Jan 2, 2015 at 16:26
  • yes. tableview.reloadData in swift Commented Jan 2, 2015 at 16:28
  • @rakeshbs I just tried it out, but unfortunately, I still have the same problem, because all the data has not loaded by the time the UITableViewController appears. Commented Jan 2, 2015 at 16:31
  • do u have a call back when all the data has been loaded? add a reload call when the loading of data is done. Commented Jan 2, 2015 at 16:38

2 Answers 2

2

As Rakeshbs was saying in the comments, what is happening here is that your query is being executed on a background thread. That means that your view is loading before the query returns and executes its callback (loading in the data).

To account for this, we can use tableview.reloadData at the end of the success callback to reload the table once we have processed the data. Code below:

NOTE: See Rakeshbs answer for a cleaner version, the only difference here is that I unwrapped the UIStoryboardSegue because it was known to cause issues when unwrapped when accessing properties in some cases.

override func prepareForSegue(segue: UIStoryboardSegue!, sender:AnyObject?){
    var cell = sender as UITableViewCell
    var text = cell.textLabel!.text
    currentScreen=text!
    println(currentScreen)
    groupConversation=[]
    var messageDisplayOne = PFQuery(className:currentScreen)
    messageDisplayOne.selectKeys(["userPost"])
    messageDisplayOne.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil{
                println("Type message \(groupConversation)")
            for object in objects {
                var textObject = object["userPost"] as String
                groupConversation.append(textObject)

            }
            var destinationController = segue.destinationViewController as UITableViewController
            destinationController.tableView.reloadData()
        } else {
            // Log details of the failure

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Are you segueing into a UITableViewController?

override func prepareForSegue(segue: UIStoryboardSegue!, sender:AnyObject?){
    //Start Spinner
    var cell = sender as UITableViewCell
    var text = cell.textLabel!.text
    currentScreen=text!
    println(currentScreen)
    groupConversation=[]
    var messageDisplayOne = PFQuery(className:currentScreen)
    messageDisplayOne.selectKeys(["userPost"])
    messageDisplayOne.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
          //Stop Spinner
          if error == nil{
            println("Type message \(groupConversation)")
            for object in objects {
                var textObject = object["userPost"] as String
                groupConversation.append(textObject)

            }
            var tableViewController = segue.destinationViewController as UITableViewController
            tableViewController.tableView.reloadData()

        } else {
            // Log details of the failure

        }

    }
}

16 Comments

Yes, I am segueing into a UITableViewController.
Well I was just typing this into my own answer, haha. You can grab the table view controller this way (through destinationViewController) and reload it from there. The only other thing you may (not sure) need to do would be to unwrap the segue, which you could do by replacing segue: UIStoryboardSegue with segue: UIStoryboardSegue! in your method header
On the line that contains tableViewController.tableView.reloadData i get an error stating that "expression resolves to unused function."
try adding parentheses () to reloadData. I'm not well versed in swift (I main Objective-C) but a quick search told me that they were required since it is a method and not a property
@PatrickBradshaw I don't think the problem is that the UITableViewController is not updating the data but rather that the problem is the fact that the data has not been retrieved by the time the segue has been completed. So maybe a spinner would work?
|

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.