Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is UICollectionViewCell's outlet nil?

I am calling self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls") again. If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard.

If you still have the problem check wether reuseIdentifier is same in dequeueReusableCellWithReuseIdentifier and in storyboard.


Just remove this line:

self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")

If you are using xib, make sure that you have added this line of code to your viewdidload.

Objective C:

[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"MyCellIdentifier"];

Swift:

collectionView.register(UINib(nibName:"MyCell", bundle: nil), forCellWithReuseIdentifier:"MyCellIdentifier")

Gotta register that nib guys!

collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCellId")

Looks like there's two ways to register and I was using the wrong one the first. I have a custom xib view so registered with the second option, and we have data!

1:

collectionView?.register(YourItemClassName.self, forCellWithReuseIdentifier: "yourIdentifier") 

2:

collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")