Search code examples
swiftparse-platformpfquery

How to retrieve File from Parse to display on PFQueryTableVIewController?


I'm trying to retrieve users profile pictures(File) from Parse onto my PFQueryTableViewController. I believe I've written my code correctly but I'm probably doing something wrong. So how do I retrieve users images from parse to display on my query?

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


if let userTweet : PFObject = self.tweets.objectAtIndex(indexPath!.row) as! PFObject {

cell.username.text = object["account"] as? String
cell.post.text = object["post"] as? String
cell.post.numberOfLines = 0

    if let Pic = object["photo"] as? PFFile {
        cell.userImage.image = UIImage(named: "Image")
        cell.userImage.file = Pic
    }

    }

Solution

  • You're not loading the PFFile for one.

    if let pic = object["photo"] as? PFFile {
        // Make sure your imageView is a PFImageView
        let imageView = cell.userImage as! PFImageView
        // I assume this is the placeholder image while your load your image files from Parse
        imageView.image = UIImage(named: "image.png")
        imageView.file = pic
        // Load the image file however you see fit
        imageView.loadInBackground(nil) 
    }
    

    As a side note, It's good practice to not capitalize your constants due to the chance they could get confused with class names in your code.