我试图在视图load方法中定义的数组中添加一些图像的集合视图。这个应用程序构建并运行良好,但是当我导航到这个名为"Portfolio“的特定部分时,这个应用程序就崩溃了,给了我这个控制台。
致命错误:在展开可选值时意外找到零
有人能从下面的代码中找出发生了什么吗?我试着用if语句包装这个问题,这样它就不会崩溃,但是图像根本不会显示。如果我只是定义了一个UIBackgroundColor,它的工作方式应该是正确的。
import UIKit
class PortfolioView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var array:[String] = []
override func viewDidLoad() {
array = ["PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7"]
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
cell.imageView.image = UIImage(named: array[indexPath.row])
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2
cell.imageView.clipsToBounds = true
cell.imageView.layer.borderWidth = 2.0
cell.imageView.layer.borderColor = UIColor.whiteColor().CGColor
return cell
}}
发布于 2015-05-15 00:46:56
从表面上看,由于您没有提供任何相关信息,并且只是猜测,您似乎已经定义了一个具有CollectionViewCell属性的类imageView,但是您没有通过连接到该imageView属性的出口连接到情节提要/ nib中的任何图像视图。因此,在加载时,您的imageView属性为零。
发布于 2016-08-04 09:22:19
在您的示例中,单元格无法找到imageView并崩溃,因为它发现它为零。在故事板上给你的imageView标签上写着"100“。然后,在单元格排出队列后,在代码中添加如下行:
let imageView = cell.viewWithTag(100) as! UIImageView imageView.image = UIImage(named: array[indexPath.item])
https://stackoverflow.com/questions/30249692
复制相似问题