Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclass of UICollectionViewCell Not Displaying

I have a UICollectionView that is displayed by clicking a table cell within a navigation controller. So the UICollectionView is the second screen in the navigation controller's stack.

Cells showed up fine in the collection view when I registered a nib and created the cell via the UICollectionViewCell class. But once I try to create a subclass for the cell, the collection view just shows up as a black screen. My project can be found here. Link to Project in Dropbox

To subclass the UICollectionViewCell, I did the following:

  • Created the .h and .m files for the subclass of UICollectionViewCell. Referenced this custom class on the nib's attribute inspector.

enter image description here

  • Registered the custom class with the cell's reuse identifier, within viewDidLoad of the view controller that displays the collection view.

    [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cvCell"];
    
  • Created an instance of the custom cell in "collectionView: cellForItemAtIndexPath:"

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath];
    

From what I've read, that should do it! But the collection view is showing up blank, can anyone help??

like image 694
Mark Newton Avatar asked Apr 02 '13 04:04

Mark Newton


1 Answers

I checked your code. You have done perfectly. Collection view with cells is showing correctly, but you cannot see that since you are not setting any of the property of the cell. Just check by setting background color of the cell in cellForItem

  cell.backgroundColor = [UIColor redColor];

If you are done everything in nib then you need to register nib instead of class. use registerNib instead of registerClass. If you are registering class you have to do everything programmatically.

like image 80
Anil Varghese Avatar answered Sep 20 '22 00:09

Anil Varghese