Search code examples
swiftparse-platformpfquerypfquerytableviewcontrolle

Parse PFQueryTableViewConntroller not loading images


I'm using parse's PFQueryTableViewController, all seems to work fine, when retrieving the date, except the UIImageView's aren't loading the files in the database. The text and everything else loads, except the file. Here is my code:

import UIKit
import Parse
import ParseUI

class PostsTableViewController: PFQueryTableViewController {

    override func queryForTable() -> PFQuery {

        let query = PFQuery(className: "Posts")
        query.cachePolicy = .CacheElseNetwork
        query.orderByDescending("createdAt")
        return query
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PostsTableViewCell

        let posterUsernameText = object?.objectForKey("createdBy") as? String
        cell.posterUsername.setTitle("\(posterUsernameText)", forState: .Normal)

        cell.msgTextView.text = object?.objectForKey("text") as? String

        let imageFile = object?.objectForKey("image") as? PFFile
        cell.cellImageView.file = imageFile
        cell.imageView?.loadInBackground()

        return cell

    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        if indexPath.row + 1 > self.objects?.count {

            return 44

        }

        let height = super.tableView(tableView, heightForRowAtIndexPath: indexPath)
        return height
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if indexPath.row + 1 > self.objects?.count {

            self.loadNextPage()
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else {

            self.performSegueWithIdentifier("showDetail", sender: self)

        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "showDetail" {



        }

    }

}

enter image description here


Solution

  • After messing around with the code for a bit, this seemed to work. Hopefully this helps others in the future and here are the changes I made:

    Instead of:

    let imageFile = object?.objectForKey("image") as? PFFile
    cell.cellImageView.file = imageFile
    cell.imageView?.loadInBackground()
    

    I should have been:

        let imageFile = object?.objectForKey("image") as? PFFile
        cell.cellImageView.file = imageFile
        cell.cellImageView?.loadInBackground()