I'm trying to add a collection view of some images within an array defined inside the view did load method. The app builds and runs fine but when I navigate to this specific section called "Portfolio" the app crashes giving me this in the console.
fatal error: unexpectedly found nil while unwrapping an Optional value
Can anyone figure out from the code below what is happening? I've tried wrapping the issue in an if statement and that keeps it from crashing but then the images won't display at all. If I simply define a UIBackgroundColor it works like it should.
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
}
}