简体   繁体   中英

UITableView Not Loading Data Initially

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

        }
    }
}

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

        }
    }
}

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

        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM