I've implemented custom UICollectionViewCell
class in Swift, created storyboard with UICollectionViewCotroller, assigned my custom controller class, custom cell class, cell id etc... Basically everything required to make UICollectionViewController work.
In storyboard cell prototype I've added few views and connected them as IBOutlets to my custom cell class:
here my IBOutlets in code (as you can see, they are connected):
I registered custom cell class in my controller too:
self.collectionView.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
When I dequeue my cell in code it returns cell of my custom type but both IBOutlets aren't initialised (equal nil
)
let cell = collectionView?.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as MyCollectionViewCell
if let path = indexPath {
// Crash here as imageView is nil
cell.imageView.image = imagesArray[path.item]
}
Crashlog:
fatal error: unexpectedly found nil while unwrapping an Optional value
if I put breakpoint on if
statement above and do po cell
I have this output:
(lldb) po cell
0x00007fa799f4cdf0
{
UIKit.UICollectionViewCell = {
UIKit.UICollectionReusableView = {
UIKit.UIView = {
UIKit.UIResponder = {
ObjectiveC.NSObject = {}
}
}
}
}
selectionView = nil
imageView = nil
}
Any ideas why IBOutlets aren't initialised?
IBOutlet weak var variableName: VarType
. Yes, I overrode initWithCoder. It is compile time error in Beta5 - you can't build your app if UIView subclass doesn't override it. – Notornis