There are several posts on nil outlets in a UICollectionViewCell class like this and this, but none of the solutions worked. Using a strong outlet instead of a weak one failed, the registerClass
solution doesn't apply since the cell doesn't use a custom XIB, the data sources and delegates are wired properly, etc.
In this case, the outlet is a UIImageView which is nil when accessed inside the UICollectionViewCell class, yet works fine when accessed outside.
UICollectionView code:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(AlbumCellIdentifier, forIndexPath: indexPath) as! AlbumCell
cell.imageView.image = getThumbnail()
cell.imageView.contentMode = .ScaleAspectFill
cell.imageView.layer.masksToBounds = true
cell.imageView.layer.cornerRadius = cell.frame.size.width / 2
return cell
}
UICollectionViewCell code:
class AlbumCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
doInit(frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
doInit(frame)
}
private func doInit(frame: CGRect) {
// Round corners
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = frame.size.width / 2
}
}
Rounding corners inside the UICollectionViewCell class fails because imageView
is nil, yet rounding corners inside of the UICollectionView class succeeds so imageView
does appear to be connected.
Why is imageView
nil inside of the UICollectionViewCell class?