0

I'm working with PFQueryTableViewController on TableViewController. The problem is: The data loads when I pull to refresh but they not load when the view loads.

I realize that all codes that have "self.tableView" are not working too like (self.tableView.reloadData) for example.

Here is my code:

import UIKit

class TableViewController: PFQueryTableViewController {
    var titulo = String()
    var sub_titulo = String()
    var url = String()
    var tipo = Int()

    @IBOutlet weak var imagemNerdmonster: UIImageView!

    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!

        if cell == nil {
            cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }

        // Extract values from the PFObject to display in the table cell
        if let titulo = object?["titulo"] as? String {
            cell?.textLabel?.text = titulo
        }
        if let sub_titulo = object?["sub_titulo"] as? String {
            cell?.detailTextLabel?.text = sub_titulo
        }
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let object = objectAtIndexPath(indexPath)
        self.titulo = object!.objectForKey("titulo") as! String
        self.sub_titulo = object!.objectForKey("sub_titulo") as! String
        self.url = object!.objectForKey("url") as! String
        self.tipo = object!.objectForKey("tipo") as! Int
        //Chama a segue
        self.performSegueWithIdentifier("detailSegue", sender: self)
    }

    //In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let svc = segue.destinationViewController as! DetailViewController
        svc.titulo = self.titulo
        svc.sub_titulo = self.sub_titulo
        svc.url = self.url
        svc.tipo = self.tipo
    }

    override func viewDidAppear(animated: Bool) {
        // Refresh the table to ensure any data changes are displayed
        super.viewDidAppear(animated)
        self.tableView.reloadData()
    }

    override func viewDidLoad() {
         self.tableView.estimatedRowHeight = 100
         self.tableView.rowHeight = UITableViewAutomaticDimension
         self.tableView.reloadData()
    }

    @IBAction func saveButton(sender: AnyObject) {
        //Return to table view
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func queryForTable() -> PFQuery {
        let query = PFQuery(className: "Conteudo")
        query.orderByAscending("createdAt")
        query.limit = 20
        //query.whereKey("currencyCode", equalTo:"EUR")
        return query
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        //Configure the PFQueryTableView
        self.parseClassName = "Conteudo"
        self.textKey = "Titulo"
        self.pullToRefreshEnabled = true
        self.paginationEnabled = true
        self.objectsPerPage = 20
    }
}

enter image description here

1 Answer 1

1

add this method to your class:

   override func viewWillAppear(animated: Bool) {
    loadObjects()
}
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.