【发布时间】:2015-05-15 00:34:44
【问题描述】:
我正在尝试在视图确实加载方法中定义的数组中添加一些图像的集合视图。该应用程序构建并运行良好,但是当我导航到名为“投资组合”的特定部分时,应用程序崩溃了,在控制台中给了我这个。
致命错误:在展开可选值时意外发现 nil
谁能从下面的代码中弄清楚发生了什么?我尝试将问题包装在 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
}
}
【问题讨论】:
-
但是你不愿意告诉我们哪个是崩溃线?
-
就是这样。我不知道是什么导致文件崩溃。感谢投反对票的好先生!
标签: ios iphone swift collectionview