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?
I was having this issue as well and the problem was in the view controllers. I'm not sure how your application is structured, so I'll just explain what I found. I'm pretty new to xcode and coding, so bear with me.
I was trying to programmatically change the text on a UILabel. Using "self.mylabel.text" worked fine until I added another view controller under the same class that didn't also include that label. That is when I got the error that you're seeing. When I added the same UILabel and connected it into that additional view controller, the error went away.
In short, it seems that in Swift, all view controllers assigned to the same class have to reference the code you have in there, or else you get an error. If you have two different view controllers with different code, assign them to different classes. This worked for me and hopefully applies to what you're doing.
Replace this line
cell.labelTitle.text = "This is a title"
with
cell.labelTitle?.text = "This is a title"
You can prevent the crash from happening by safely unwrapping
cell.labelTitle
with anif let
statement.You will still have to do some debugging to see why you are getting a nil value there though.
Almost certainly, your reuse identifier
"title"
is incorrect.We can see from the
UITableView.h
method signature ofdequeueReusableCellWithIdentifier
that the return type is an Implicitly Unwrapped Optional:That's determined by the exclamation mark after
AnyObject
:So, first thing to consider is, what is an "Implicitly Unwrapped Optional"?
The Swift Programming Language tells us:
So, basically, something that might have been nil at one point, but which from some point on is never nil again. We therefore save ourselves some bother by taking it in as the unwrapped value.
It makes sense in this case for
dequeueReusableCellWithIdentifier
to return such a value. The supplied identifier must have already been used to register the cell for reuse. Supply an incorrect identifier, the dequeue can't find it, and the runtime returns a nil that should never happen. It's a fatal error, the app crashes, and the Console output gives:Bottom line: check your cell reuse identifier specified in the .storyboard, Xib, or in code, and ensure that it is correct when dequeuing.
Same message here, but with a different cause. I had a
UIBarButton
that pushed a segue. The segue lacked an identifier.I don't know is it bug or something but your labels and other UIs does not initialized automatically when you use custom cell. You should try this in your
UICollectionViewController
class. It worked for me.