0

我正在使用 Parse,我有一个名为“GolfScorecard”的类,它有几个键,包括一个“scorecardImage”键,它是一个 PFFile。我正在尝试将所有这些高尔夫记分卡信息显示在 tableViewCell 中。我可以获取要在单元格中显示的其余记分卡类信息(例如日期、高尔夫球场名称、分数等),但在显示 scorecardImage 时遇到问题。

我正在继承 PFObject。当我查询类时,我会将对象附加到“GolfScorecard”(子类)数组中,然后包含我从 Parse 查询的所有数据。

这个子类有一个 PFFile 类型的属性“scorecardImage”。有没有办法在不使用“getDataInBackgroundWithBlock”的情况下将 PFFile 转换为UIImage?问题是当我使用“getDataInBackgroundWithBlock”时,图像在错误的单元格中与错误的高尔夫球场名称、分数、日期等不匹配。由于某种原因,当我使用时,所有这些信息都没有保持在一起“getDataInBackgroundWithBlock”方法。

  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)

                }

            }

提前致谢!

- 这是我设置单元格图像视图的代码

    let pfImage = scorecardData[indexPath.row].scorecardImage

    pfImage.getDataInBackgroundWithBlock({
        (result, error) in

        if result != nil {

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

        } else {

            print(error)

        }
    })
4

1 回答 1

0

我最终解决了这个问题。我将自定义单元格设为 PFTableViewCell 并将图像转换为 PFImageView。然后我使用了以下代码,它可以将 PFFile 转换为 UIImage,因为 PFImageView 可以接受 PFFile。这使得从 Parse 下载 PFFile 更加顺畅。

    cell.scorecardCellImage.file = scorecardData[indexPath.row].scorecardImage
    cell.scorecardCellImage.loadInBackground()
于 2015-11-27T00:20:41.110 回答