This question already has an answer here:
I was using an UICollectionView
in Swift but I get when I try to change the text of the cell's label.
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
return 5
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("title", forIndexPath: indexPath) as TitleCollectionViewCell
// Next line: fatal error: unexpectedly found nil while unwrapping an Optional value
cell.labelTitle.text = "This is a title"
return cell
}
Does anyone know about this?
Check if the cell is being registered with
self.collectionView.registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
. If so, then remove that line of code.See this answer for more info: Why is UICollectionViewCell's outlet nil?
"If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard."
:) hopes it will help for some struggled people .
I had the same problem on Xcode 7.3. My solution was to make sure my cells had the correct reuse identifiers. Under the table view section set the table view cell to the corresponding identifier name you are using in the program, make sure they match.
I was having the same issue and the reason for it was because I was trying to load a UIImage with the wrong name. Double-check
.setImage(UIImage(named: "-name-"
calls and make sure the name is correct.Nil Coalescing Operator can be used as well.
I got his error when i was trying to access
UILabel
. I forgot to connect theUILabel
toIBOutlet
in Storyboard and that was causing the app to crash with this error!!