2015-04-26 58 views
0

我正在处理一个Parse查询,该查询抽取基于地理位置的名称和照片,然后将它们加载到表视图上。查询的第一部分即将通过,没有问题。我被困在拉/加载照片的第二部分。PFFile不加载到表视图

我可能会因为在queryForTable方法中缺少对图像的调用而出错。在这里我已经尝试了includeKey(失败了,因为它不是指向另一个对象的指针);我还使用getDataInBackgroundWithBlock来调用图像。在每种情况下,我会尝试加载查询的对象到像cell.imageData.image = photo这样的线视图上。

但是不同的SO账户也表明这个调用可以在tableView方法中进行,这就是我在下面的内容。我梳理了SO,Parse.com和其他资源,但我还没有遇到过解决方案。希望有人能帮助:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject?) -> Stores! { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! Stores 

    if let object = object { 
     let Name = object.valueForKey("Name") as? String 
     cell.storeList.text = Name 

     let photo = object.objectForKey("Photos") as? PFFile 
     photo?.getDataInBackgroundWithBlock {(imageData: NSData!, error: NSError!) -> Void in 
      if error == nil { 
       let image = UIImage(data: imageData) 
       cell.storeImage.file = photo 
      } 
     }} 

    return cell as Store 
+0

将背景块作为单元格dequeu运行是一个坏主意。 – ericgu

+0

您可以使用我的黑客: http://stackoverflow.com/questions/28649257/parse-pfquerytableviewcontroller-pfquerytableviewcell-subclass-breaks-pfimag – ericgu

+0

谢谢,埃里克!我试图实施黑客攻击,但图像也没有出现。我已经删除了“getDataInBackgroundWithBlock”。 – Christine

回答

0

什么终于为我工作是将文件属性中的呼叫内,这样的:

cell.imageView.loadInBackground {(image: UIImage!, error: NSError!) -> Void in 
    if error == nil { 
    cell.imageView.file = object!.objectForKey("Photos") as? PFFile 
    } 

重访解析文档显示它是'.file',而不是'.image'。另外,我使用'loadInBackground',而不是'loadInBackgroundWithBlock'来进行异步调用。希望这可以帮助那些对同一问题感到困惑的人。

感谢@BHendricks,@ericgu,@danh分享他们的观点!尝试所有的建议绝对让我更接近我的答案。

0

而不是使用cell.storeImage.file = photo的,你应该使用image

您完成区块内所以,你应该有:

let image = UIImage(data: imageData) 
cell.storeImage.file = image 
+0

感谢您的建议。我建议固定。不幸的是,图片仍然没有显示。 – Christine

+0

是正在执行的getData调用的完成块吗? – BHendricks

+0

该调用在一定程度上得到执行。用断点测试它,没有指示返回对象。 – Christine