2015-04-23 26 views
0
@IBOutlet weak var itemImage: PFImageView! 

override func tableView(tableView: (UITableView), cellForRowAtIndexPath indexPath: (NSIndexPath!), object: PFObject!) -> PFTableViewCell? { 

    var cell = tableView.dequeueReusableCellWithIdentifier("PFTableViewCell") as! ItemViewCell! 

    let items = object as! Items 

    let thumbnail = items.valueForKey("itemImage") as! PFFile! 
    cell.itemImage?.file = thumbnail 

    return cell 
} 

當我運行這個時,PFTableViewController只是無限加載。解析不加載到PFTableViewCell的文件圖像

註釋掉:

cell.itemImage .file =縮略圖

將加載TVC但沒有加載的圖像。我爲故事板中的每個連接/插座都起作用。

回答

0

我認爲你需要調用getDataInBackground來獲取實際的PFFile數據,然後你可以從你的查詢中得到的NSData對象創建一個UIImage。

if let pic = items.objectForKey("itemImage") as? PFFile 
{ 
    pic.getDataInBackgroundWithBlock(
    { (data: NSData?, error: NSError?) -> Void in 

     if((error) == nil && data != nil) 
     { 
      if let image = UIImage(data: data!) 
      { 
       cell.itemImage = image } 
      }     
      else { NSLog("Error loading profile image in cell") } 
     } 
}) 

這是假設你的UITableViewCell子類有一個屬性「itemImage」。

+0

您仍需要重新加載與您加載圖像單元格中的索引路徑的實現代碼如下,否則即使其下載它不會顯示 – Joseph

1

如果您嘗試在tableView中的單元格中顯示UIImageView中的圖像,則給出的答案會引發錯誤,因爲您僅將其指定爲UIImage的實例。

要解決這個問題,您需要將UIImageView插座分配給.image ,如下圖所示;

cell.itemImage.image = image 

在我的測試項目中工作的完整解決方案如下;

if let pic = items.objectForKey("itemImage") as? PFFile 
{ 
    pic.getDataInBackgroundWithBlock(
    { (data: NSData?, error: NSError?) -> Void in 

     if((error) == nil && data != nil) 
     { 
      if let image = UIImage(data: data!) 
      { 
       cell.itemImage.image = image 
      } 
     }     
      else { NSLog("Error loading profile image in cell") } 
     } 
}) 

編輯:正如在以前的帖子的評論中提到,你也將需要重新加載的tableView之前的數據將顯示爲的tableView本例中的圖像之前幾乎總是加載。

您可以通過添加;

tableView.reloadData() 

在你的函數結束