2015-04-26 34 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