UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectionView

28,768

Solution 1

If you've already created your UICollectionView in Storyboard, connected your dataSource and delegate, and you have added all of the required methods:

  • numberOfItemsInSection
  • numberOfSectionsInCollectionView (not a required method - refer to this comment)
  • cellForItemAtIndexPath

Then the registerClass / registerCell method isn't required. However, if you need to reuse a view, data, or cells then you should use those methods so that iOS can populate your UICollectionView as needed. This can also be done in your Storyboard by setting the Prototype Cell (the same principle as the registerClass method).

Also, if you're looking for a good explanation on what registerCell does and how to use it check out this link and scroll to the bottom section titled "Cell and View Reuse."

Solution 2

Agree with RazorSharp's answer and wanted to point out that that key phrase for me in the Techtopia link is:

If the cell class was written in code, the registration is performed using the registerClass: method of UICollectionView, otherwise use registerNib

Share:
28,768
Janusz Chudzynski
Author by

Janusz Chudzynski

Currently software engineer at Camtasia team. Former research associate at University of West Florida, Author of two ebooks about mobile programming. Instructor of mobile programming course at the University of West Florida. Recipient of Apple scholarship. Owner of software development company Izotx LLC. Open to project opportunities.

Updated on May 19, 2020

Comments

  • Janusz Chudzynski
    Janusz Chudzynski almost 4 years

    What's the role of registerClass:forCellWithReuseIdentifier: method? According to Apple's developer documentation it's supposed to

    "Register a class for use in creating new collection view cells."

    When I try to use it my project I get a black collection view. When I delete it everything works fine.

    #define cellId @"cellId"
    #import "ViewController.h"
    #import "Cell.h"
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
    @property(strong, nonatomic)NSMutableArray * photoArray;
    
    
    @end
    
    @implementation ViewController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSLog(@"%@",_photoArray);
        _photoArray = [[NSMutableArray alloc]initWithCapacity:0];
        [_collectionView registerClass:[Cell class] forCellWithReuseIdentifier:cellId];
        for(int i=1;i<=12;i++)
        {
            NSString * imgName = [NSString stringWithFormat:@"%d.png",i];
            UIImage *img  = [UIImage imageNamed:imgName];
            [_photoArray addObject:img];
        }
    }
    
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return _photoArray.count;
    }
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
        return 1;
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        Cell* cell = [_collectionView  dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        cell.cellImage.image = [_photoArray objectAtIndex:indexPath.row];
        return cell;
    }
    
  • So Over It
    So Over It almost 11 years
    Thanks @RazorSharp. However, would just like to point out numberOfSectionsInCollectionView: is not a 'required method'. As per Apple Documentation, If you do not implement this method, the collection view uses a default value of 1.
  • Sam Spencer
    Sam Spencer almost 11 years
    @SoOverIt Good point! I had forgotten that, please check out my edit.
  • omz
    omz almost 11 years
    The main point is that you can connect a prototype cell directly in a storyboard, which fulfills the same role as calling registerClass:.... Memory usage or cell reuse don't really have anything to do with this. A collection view never loads cells into memory that aren't on screen, and if the cell identifier wasn't actually registered, the line where he dequeues a cell would crash.
  • Nitya
    Nitya over 10 years
    Perfect Answer!! save lots of my time Thank you SAM!!