问题
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
}
Original question
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
.
Not a possible duplicate:
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.
回答1:
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, axib
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
orUICollectionViewController
. 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.
回答2:
You can assign Nib to Collection view cell with an identifier as follows :
self.collectionView.register(UINib(nibName: "nibName", bundle: nil), forCellWithReuseIdentifier: "cell")
Hope it helps.
来源:https://stackoverflow.com/questions/44105665/whats-wrong-with-register-forcellwithreuseidentifier-on-uicollectionview