I'm writing an app in which I have a horizontal collection view within a table view cell.
When trying to set the text in the cell programmatically, the app crashes with the fatal error: unexpectedly found nil while unwrapping an Optional value
error message.
To me, and to a much more experienced cocoa-touch programmer, this suggested that my IBOutlets were not connected correctly, and so cell.filmTitle
would return nil when explicitly unwrapped. So I unconnected all my IBOutlets to the cell XIB, and removed all the code behind them, before recoding the IBOutlets and reconnecting them. But the error was still there.
The code for the custom cell is:
class homeWhatsOnCellectionCell: UICollectionViewCell {
@IBOutlet var filmTitle: UILabel!
@IBOutlet var filmPoster: UIImageView!
}
And the code for the collection view datasource is:
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
var cell: homeWhatsOnCellectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("Home Whats On Collection View Cell", forIndexPath: indexPath) as homeWhatsOnCellectionCell
cell.filmTitle.text = "The Hunger Games: Catching Fire"
cell.filmPoster.image = UIImage(named: "The Hunger Games.jpg")
return cell
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
return 1
}
Edit: From a conversation with Nate Cook, I can say that filmTitle
is the cause of the problem, with it being nil
despite being connected properly to a label. Also, dump(cell)
outputted "-" to the console. Thanks to anybody who can help.
Edit 2: Through further investigation, I have found that the cell is not being seen at all. When I removed the IBOutlets, and tried to simply show 10 cells as they appear in the XIB, they don't show up at all, and all I get is ten blank cells of the same size. It is showing some cells, as the scrollbar changes size and scrolls based on how many cells are being displayed.