Cannot load image from PARSE into PFTableViewCell's imageView property

270 Views Asked by At

I'm working with the PFQueryTableViewController and setting it up to find only friendships for this user which has been approved or sent to him.

"fromUser" and "toUser" are pointers to the user class, where I need the username and profilePicture from for each of the users contained in the queries results.

Now I try to fetch those in my cellForRowAtIndexPath method and load the image:

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


    let cellIdentifier = "contactCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! PFTableViewCell

    if let user = object?["fromUser"] as? PFUser {

        user.fetchInBackgroundWithBlock({ (user, error) -> Void in

            if error != nil {

                println("Could not fetch user object")
            }

            let user = user as! PFUser
            cell.textLabel?.text = user.username!
            cell.imageView?.file = user["profilePicture"] as? PFFile
            cell.imageView?.loadInBackground()
        })



    } }

Getting the username to display in the tableView works just fine, but the image is actually never loaded. I tried different approaches to get the image loaded, but my cell's imageView property is always nil.

  • The prototype's class is set to PFTableViewCell
  • The controller is linked to the view in storyboard

Please let me know, if you guys have any idea why this built in property is nil and how to fix that.

Thanks,

2

There are 2 best solutions below

0
On

The PFTableViewCell does not work well with standard styles. So you have to make your cell a custom cell.

These re the steps I took to make it work (in Storyboard):

  • subclass PFTableViewCell with you own class
  • in the storyboard, customize the prototype cell using a custom cell
  • drag a UIImageView from the palette but then set its class as PFImageView
  • connect it to an IBOutlet in your PFQueryTableViewController subclass
  • implement cellForRowAtIndexPath: in the PFQueryTableViewController the way you did (your code was OK).

That way worked for me.

0
On

Well I found a workaround which actually works quite good:

  1. Create a prototype cell in your TableView (Storyboard) and set it's class to the normal "UITableViewCell"
  2. Set the reuseIdentifier property of this cell to a value of your liking
  3. Let your custom cell's file owner (I created a nib file for this cell) to be a subclass of PFTableViewCell
  4. Create custom outlets for the textLabel and imageView
  5. Register that Nib for the reuseIdentifier set in Step 2 in your TableViewController
  6. Finally, use that class in your cellForRowAtIndexPath method like this:

    let cell: PeopleTableViewCell! = tableView.dequeueReusableCellWithIdentifier("peopleCell") as? PeopleTableViewCell

I hope this will fix your problems too.

Regards,

[Edit]: It seems like SO doesn't like my code snippet to be formatted. It's embedded in code tags..