swiftparse-platformpfquerytableviewcontrolle

Data from Parse not loading into PFQueryTableViewController


I am using a PFQueryTableViewController to try to load data into a tableview.

EDIT: link to photo referred to just below: http://i61.tinypic.com/x5626a.png

To my knowledge everything is set up correctly, but when running in the simulator, no images or text appear. The query loads the correct number of cells though: There are 14 objects in my "Post" class, and the simulator loads 14 table cells, but they all just appear as in the photo above. There are also 2 labels in the tableview (which I pre-populated in the attributes inspector with text) and that text doesn't show up either.

EDIT 2:

I have figured out how to display text in the cell, but I can not get how to display the image. This is my code in my cellForRowatIndexPath:

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell? {
    //4
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! DataCell

    let Postobject1 = object as! Post

    cell.shortDescriptionLabel.text = Postobject1["short_decription"] as? String
    cell.userUsername.text = Postobject1["user2"] as? String

    var thumbnail: PFFile = Postobject1["fichierimage"] as! PFFile
    cell.contentImage.file = thumbnail




    return cell

  }

the code works correctly to display the strings, shortDescriptionLabel and userUserName. But it does not work to load the image. I took this code:

    var thumbnail: PFFile = Postobject1["fichierimage"] as! PFFile
    cell.contentImage.file = thumbnail

from the Parse site, here: http://i60.tinypic.com/2ziq0hu.png

however when I clicked "swift" it said there is no swift example, so that is my translation of the code. I've never programmed in Obj-C before so maybe I translated it wrong. Could someone help with a swift translation please?


Solution

  • I have finally found an answer after about 9 hours. I will post all of the details here for people who find this in the future. Hopefully they won't struggle as long as I did :)

    The goal was to load 2 images and 2 Strings from my class "Post" in Parse, and display these in a PFTableViewCell. One of these images was from relational data, from a pointer in the "Post" class.

    Here is my code for my "Post" PFObject class:

            import Foundation
            import Parse
            import ParseUI
            import UIKit
    
        class Post: PFObject, PFSubclassing {
    
    @NSManaged var image: PFFile
    
    @NSManaged var short_decription: String?
    
    
    
    
    //1
    class func parseClassName() -> String {
        return "Post"
    }
    
    //2
    override class func initialize() {
        var onceToken: dispatch_once_t = 0
        dispatch_once(&onceToken) {
            self.registerSubclass()
        }
    }
    
    override class func query() -> PFQuery? {
        let query = PFQuery(className: Post.parseClassName())
        query.includeKey("from_user")
        query.orderByDescending("createdAt")
       // query.whereKey("early_semester", equalTo: true)
        return query
    }
    
       init(short_decription: String?) {
        super.init()
    
    
    
    
    
    
    
    
    }
    
     override init() {
        super.init()
    
    
    
        }
    
     }
    

    Here is the code for my tableCell:

        import UIKit
        import ParseUI
        import Parse
    
      class DataCell: PFTableViewCell {
    
    
    @IBOutlet weak var shortDescriptionLabel: UILabel!
    
    @IBOutlet weak var UserProfilePhoto: PFImageView!
    
    @IBOutlet weak var contentImage: PFImageView!
    @IBOutlet weak var userUsername: UILabel!
    
    
    
     }
    

    Here is the code for my PFQueryTableViewController:

         import UIKit
         import Parse
         import ParseUI
         import Foundation
    
         class new: PFQueryTableViewController {
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
       loadObjects()
    }
    
    //1
    override func viewWillAppear(animated: Bool) {
    
        loadObjects()
    
    }
    
    //2
    override func queryForTable() -> PFQuery {
        let query = Post.query()
        return query!
    
    }
    
    //3
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell? {
        //4
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! DataCell
    
    
    
        let PostObject = object as! Post
    
    
         // assign label in tablecell text from my "Post" class 
        cell.shortDescriptionLabel.text = PostObject["short_decription"] as? String
        cell.userUsername.text = PostObject["user2"] as? String
    
        // loading image saved in my "Post" class
        var thumbnail: PFFile = PostObject["fichierimage"] as! PFFile
        cell.contentImage.file = thumbnail
        cell.contentImage.loadInBackground()
    
    
        /* loading image that is relational data. My "Post" class has
       a pointer column "from_user" that points to the "User" class,
         and this is loading the profile photos from the User class. 
       this is possible because I used  "query.includeKey("from_user")" in my query.*/
    
        var profilePhoto: PFFile = PostObject.objectForKey("from_user")!.objectForKey("profilePicture") as! PFFile
        cell.UserProfilePhoto.file = profilePhoto
        cell.UserProfilePhoto.loadInBackground()
    
        return cell
    
     }
    
    
    
      }