swiftuitableviewparse-platformpffile

PFFile as UIImageView


I'm using Parse and I have a class named "GolfScorecard" that has several keys including a "scorecardImage" key, which is a PFFile. I'm trying to get all of this golf scorecard info to display in a tableViewCell. I can get the rest of the scorecard class info to display in the cell (such as date, golf course name, score, etc) but I'm having trouble with displaying the scorecardImage.

I'm subclassing PFObject. When I query the class I then append the objects to an array of "GolfScorecard" (subclass) that then contains off all of the data I queried from Parse.

This subclass has a property "scorecardImage" that is of type PFFile. Is there a way to convert the PFFile to a UIImage WITHOUT using "getDataInBackgroundWithBlock"? The problem is when I'm using "getDataInBackgroundWithBlock" the images are mismatched in the wrong cell with the wrong golf course name, score, date, etc. For some reason all of this info isn't staying together when I'm using the "getDataInBackgroundWithBlock" method.

  var scorecardData = [GolfScorecard]()

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

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

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"

    cell.dateCellLabel?.text = dateFormatter.stringFromDate(scorecardData[indexPath.row].date)

    cell.scoreCellLabel?.text = "\(scorecardData[indexPath.row].score)"

    cell.golfCourseCellLabel?.text = scorecardData[indexPath.row].golfCourse

    return cell
 }

 //THIS IS MY QUERY
 func loadUserScorecardData() {
    scorecardData.removeAll()

    let query = GolfScorecard.query()
    query!.whereKey("golfer", equalTo: PFUser.currentUser()!)

    query!.findObjectsInBackgroundWithBlock { (scorecards: [PFObject]?, error: NSError?) -> Void in
        if error == nil {
            for object:PFObject in scorecards! {
                if let object = object as? GolfScorecard {
                    self.scorecardData.append(object)
                    print(self.scorecardData)
                    print(self.scorecardData.count)

                }

            }

THANKS IN ADVANCE!

-HERE IS MY CODE FOR SETTING THE CELL'S IMAGEVIEW

    let pfImage = scorecardData[indexPath.row].scorecardImage

    pfImage.getDataInBackgroundWithBlock({
        (result, error) in

        if result != nil {

            cell.scorecardCellImage.image = UIImage(data: result!)

        } else {

            print(error)

        }
    })

Solution

  • I ended up solving this issue. I made my custom cell a PFTableViewCell and turned the image into a PFImageView. I then used the following code, which works around converting the PFFile into a UIImage because PFImageView can accept a PFFile. This makes the downloading of the PFFile from Parse a lot smoother.

        cell.scorecardCellImage.file = scorecardData[indexPath.row].scorecardImage
        cell.scorecardCellImage.loadInBackground()