2015-10-15 56 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

相关问题