2015-10-15 60 views
0

我想實現一個集合視圖控制器,並大量失敗...我如何擺脫這個解包錯誤?它在我的cellForRowAtIndexPath函數中創建我的collectionView單元格時發生。UICollectionViewController - 意外發現零,同時展開一個可選值

這是代碼。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

let cell = picturesCollectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as? PhotoBrowserCollectionViewCell 

    if let photoObject = pictureObjects.objectAtIndex(indexPath.row) as? PictureElement { 

     let title = photoObject.title ?? "" // if photoObject.title == nil, then we return an empty string 

     let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(photoObject.timeStamp)) 
     let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject) 

     let author = photoObject.author ?? "" 

     let issueNumber = photoObject.issueNumber ?? "" 
     let volumeNumber = photoObject.volumeNumber ?? "" 

     let nodeID = photoObject.nodeID ?? 0 

     let imageURL = photoObject.imageURL ?? "" 

     cell!.request?.cancel() 

     // Using image cache system to make sure the table view works even when rapidly scrolling down the screen. 
     if let image = self.imageCache.objectForKey(imageURL) as? UIImage { 

      cell!.imageView.image = image 

     } else { 

      cell!.imageView.image = nil 
      cell!.request = Alamofire.request(.GET, imageURL).responseImage { response in 
       if let image = response.result.value { 
        self.imageCache.setObject(response.result.value!, forKey: imageURL) 
        cell!.imageView.image = image 

       } else { 
       } 
      } 
     } 
    } else { 
} 
    return cell! 

}

而且,我已經嘗試過使用保護,讓語句,並沒有奏效。那就是:

 guard let cell = picturesCollectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as? PhotoBrowserCollectionViewCell else { 

     print("cell could not be initialized as PhotoBrowserCollectionViewCell, hence it will be casted to another default UICollectionViewCell type") 
     return collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) 


    } 
+1

需要更多信息,你可以發佈你的錯誤信息。 – Barrett

+0

這就是我所能得到的。我一直無法找出其他的東西。你認爲這可能是什麼(即使你不確定)? –

+1

哪一行導致錯誤? – t4nhpt

回答

0

假設你使用的是故事板...

  1. 確保您設置在故事板的細胞相匹配PhotoBrowserCellIdentifier價值的重用標識符。

  2. 確保您將故事板中單元格的類設置爲PhotoBrowserCollectionViewCell。

+0

是的,我已經設置了。其他可能的解決方案? –

+1

最後一個可能,你是否將IBOutlet連接到你的'picturesCollectionView'?嘗試使用'po'命令並在崩潰的行打印任何值以查看零變量。 – t4nhpt

相關問題