156

I have created a custom UICollectionViewCell in Interface Builder, binded views on it to the class, and then when I want to use and set a string to the label on the string, tha label has a nil value.

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
}

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {

    var cell: LeftMenuCollectionViewCell
    cell = collectionView.dequeueReusableCellWithReuseIdentifier("ls", forIndexPath: indexPath) as LeftMenuCollectionViewCell
    println(cell.label) // <- this is nil, why??
    cell.label.text = "asd"

    return cell
}

And the subclassed cell:

class LeftMenuCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
}
János
  • 32,867
  • 38
  • 193
  • 353
  • Is there a reason you're using the explicit "!" in addition to type names everywhere? Seems redundant, esp in the IBOutlets. The only place it might matter is where you dequeue the cell as LeftmenuCollectionViewCell, but you don't do it there.. Can you println or NSLog 'cell' itself? – mc01 Aug 06 '14 at 16:28
  • 1
    If I remove "!" or use "?" in class definition, I get compiler error or crash. "!" is the default when you bind it. – János Aug 06 '14 at 16:32
  • ant the cell itself: > – János Aug 06 '14 at 16:42
  • All outlets hooked up in IB? Do the other outlets work? Don't see any other obvious issues, sorry. – mc01 Aug 06 '14 at 17:19
  • [Basic example for setting up a Collection View](http://stackoverflow.com/a/31735229/3681880) – Suragch Jul 30 '15 at 23:10

8 Answers8

339

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.

János
  • 32,867
  • 38
  • 193
  • 353
  • 21
    Thank you! Spent more than an hour on this as was using a custom cell. – DogCoffee Aug 18 '14 at 13:27
  • i have the same problem but never used this line of code, you have any idea why i get the same exception? – Dekel Maman Sep 19 '14 at 05:55
  • thanks. this solved it for me. i had a storyboard and i manually added a view controller. the boilerplate code included the line you mentioned. even though it seemed correct (correct identifier & class) it refused to connect the outlets. now it works. sweet! – Joris Weimar Oct 09 '14 at 19:36
  • @Dekel Maman just make sure you have setted the right custom class and module for your cell in interface builder – Gomino Nov 05 '14 at 17:47
  • 14
    **If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard.** <- This one really Help me out – Sruit A.Suk Mar 06 '15 at 14:06
  • Same here. Thanks a million. (As in the number of hours I might have spent tracking this down otherwise.) – quickthyme Mar 27 '15 at 23:25
  • Ha ha, I just searched for this and found that I had already upvoted this answer some previous time I don't remember. – bugloaf Sep 29 '15 at 21:46
  • 6
    GAH, what a horrible thing to have in the UICollectionViewController template!!!!! Thanks, Janos. – Kenny Wyland Nov 19 '15 at 00:02
  • Apple needs to take away that bear trap! Put a comment in not to use if using storyboard! – anders32 Feb 02 '18 at 06:20
  • Straight up the documentation doesn't even tell you that – Script Kitty May 02 '18 at 17:54
  • What about if it is not in the storyboard? I have a custom xib and I am having this problem. If I remove the register(...) I get an exception in the AppDelegate :/ – Johana Lopez 1327 Apr 16 '19 at 17:13
  • It was my fault. I was setting the datasource and the delegate before registering the cell... – Johana Lopez 1327 Apr 16 '19 at 17:21
  • whats really annoying is the code to register a cell is boiler plated in when you subclass a collection view controller – philthomas26 Jun 09 '19 at 18:16
  • Why would registering twice cause an issue? I'm not getting that.. – ScottyBlades Jul 01 '21 at 05:36
58

Just remove this line:

self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
Undo
  • 25,519
  • 37
  • 106
  • 129
能蟹仔
  • 767
  • 5
  • 10
  • 3
    I don't understand why this was down voted. This was the answer to my problem. – Félix Simões Jun 24 '15 at 09:17
  • 1
    This is the correct answer, and should be marked accordingly by the person who asked the question. Would also be great to understand why it works :) – Kiko Lobo Jan 19 '16 at 07:40
  • 4
    Guess it was downvoted cause the question was answered in Aug'14 itself... while this answer just copies the answer again here, after several months. – Nitin Nain Mar 17 '16 at 16:28
  • 1
    But when I don't use it, it gives me error Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] – Shaheera Jun 30 '16 at 11:04
  • An explanation for why this line is not needed would be welcome. It's currently added by the Xcode boilerplate for a new collection view. – pkamb Dec 08 '18 at 03:53
57

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")
Vineeth
  • 1,720
  • 1
  • 16
  • 23
  • do you have to register both class and nib, or only nib is enough? – RainCast Jun 15 '16 at 22:13
  • 3
    I just registered the nib and it solved my problems. – Amelia Nov 05 '16 at 20:25
  • 1
    Looks like there's two ways to register and I was using the wrong one... collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier") collectionView?.register(YourItemClassName.self, forCellWithReuseIdentifier: "yourIdentifier") – Sergio Jul 25 '19 at 16:05
  • registering the cell in the wrong way ... wasted half of my life on this shitty small error... – Wahab Khan Jadon Nov 03 '22 at 08:36
17

Gotta register that nib guys!

collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCellId")
Michael
  • 9,639
  • 3
  • 64
  • 69
15

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")
Sergio
  • 2,346
  • 2
  • 24
  • 28
-1

I had a similar problem, but my mistake was that I didn't delegate CollectionViewCell to be able to change the label text..

justRadojko
  • 257
  • 5
  • 19
-1

I think that best solution is to directly use from storyboard where add a CollectionView, in alternative you need to remove a CollectionViewCell from your CollectionView in storyboard and register a cell with the following command:

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

toshiro92
  • 1,287
  • 5
  • 28
  • 42
dgalluccio
  • 85
  • 5
-1

You didn't register your cell,

fileprivate let yourIdentifier = ""yourIdentifier"
super.viewDidLoad() { 

//here you need to register cell collectionView?.register(NameOfYourClass.self, forCellWithReuseIdentifier: "yourIdentifier") }