I'm working with an UICollectionView
. As dequeueReusableCell(withReuseIdentifier:for:)
expects You must register a class or nib file using the register(_:forCellWithReuseIdentifier:)
method before calling this method, I added a line in my viewDidLoad
func as
self.collectionView!.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
Now when I'm using the cell for dequeuing and configuring, I'm getting error and app crashes.
fatal error: unexpectedly found nil while unwrapping an Optional value
This is my DataSource method:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
for: indexPath) as! PhotoCollectionViewCell
let aPhoto = photoForIndexPath(indexPath: indexPath)
cell.backgroundColor = UIColor.white
cell.imageView.image = aPhoto.thumbnail //this is the line causing error
return cell
}
And this is my PhotoCollectionViewCell
class
class PhotoCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView! //I double checked this IBOutlet whether it connects with Storyboard or not
}
Now comes the interesting part.
I'm using a prototype
cell in the UICollectionView
and I set a reusable identifier
from attributes inspector. Also I changed the custom class from identity inspector to my PhotoCollectionViewCell
.
I searched for the same issue and found out that when using prototype cell, deleting
self.collectionView!.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
from code will work. I gave it a try and it works.
But I'm curious to know the reason behind this issue. I couldn't reproduce the same issue with
UITableView
but withUICollectionView
.
This UICollectionView's cell registerClass in Swift is about how to register class in UICollectionView
. But my question doesn't expect how to register. My question is about an issue that isn't true with UITableView
class but with UICollectionView
only. I'm expecting the actual difference between this conflicting issue.
Use a cell registration to register cells with your collection view and configure each cell for display. You create a cell registration with your cell type and data item type as the registration's generic parameters, passing in a registration handler to configure the cell.
An object that manages an ordered collection of data items and presents them using customizable layouts.
There are 3 ways to register a cell (either for UITableView
or UICollectionView
).
You can register a class. That means the cell has no nib or storyboard. No UI will be loaded and no outlets connected, only the class initializer is called automatically.
You can register a UINib
(that is, a xib
file). In that case the cell UI is loaded from the xib and outlets are connected.
You can create a prototype cell in the storyboard. In that case the cell is registered automatically for that specific UITableViewController
or UICollectionViewController
. The cell is not accessible in any other controller.
The options cannot be combined. If you have a cell prototype in the storyboard, you don't have to register again and if you do, you will break the storyboard connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With