解析图像 - > UITableViewCell不显示(Swift)

时间:2015-07-06 21:23:22

标签: ios swift parse-platform

我很难将图片存储在Parse中以显示在我的UITableViewCell中。正在从存储在Parse对象(名称等)中的其他数据填充单元格 - 只是图像无法正确显示。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")

    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    if let client: PFObject = self.clientList[indexPath.row] as? PFObject {
      let forename: String = client["forename"] as! String
      let surname: String = client["surname"] as! String
      let fullname: String = forename + " " + surname
      cell.textLabel?.text = fullname

      cell.detailTextLabel?.text = client.objectId

      let userImageFile = client["image"] as? PFFile
      if let imageFile = userImageFile {
        println(imageFile)
        imageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
          if error == nil {
            if let imageData = imageData {
              self.image = UIImage(data: imageData)!
            }
          }
        })
    // THIS DOESN'T DISPLAY AN IMAGE WHEN RUN
        cell.imageView?.image = image
      } else {

    // THIS IS A LOCAL IMAGE DISPLAYED IF THE PARSE OBJECT DOESN'T CONTAIN AN IMAGE FILE. THIS DOES DISPLAY CORRECTLY WHEN CALLED.
        let image = UIImage(named: "userMale")
        cell.imageView?.image = image
        println("Local Image")
      }
    }

    cell.imageView?.layer.cornerRadius = 40
    cell.imageView?.layer.masksToBounds = true
    cell.imageView?.layer.borderColor = UIColor.lightGrayColor().CGColor
    cell.imageView?.layer.borderWidth = 1
    return cell
}

1 个答案:

答案 0 :(得分:0)

当您按照所示插入' cell.imageView?.image = image'时,您的背景检索不完整。您需要在后台完成后使用发布通知,然后在收到通知后设置单元格。如果您在将图像分配给单元格之前对图像执行println,则会发现它显示0到0的大小。通知示例在example

中的示例代码中