Search code examples
iosobjective-cios6uicollectionview

Storyboard, UICollectionview, unrecognized selector sent to instance


I have a storyboard hosting a UICollectionview, in the collectionviewcontroller I implement the

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
   itemViewCell *itemCell =
   [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
                                         forIndexPath:indexPath];

   return itemCell;
 }

but on the dequeueReusableCellWithReuseIdentifier the app throws this exception :

 [__NSCFConstantString initWithFrame:]: unrecognized selector sent to instance 0x7443c60

So my first inclincation is to look at the custom cell I am creating, just to make sure I have an implementation for that method , and I actually do

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       self.backgroundColor = [UIColor whiteColor];
    }
    return self;
} 

And of course I am registering that cell in the viewdidload

 [super viewDidLoad];   
 [self.collectionView registerClass:[CellIdentifier class]
       forCellWithReuseIdentifier:CellIdentifier];

But I don't think this is related, because I put a breakpoint on the initWithFrame method and it never hots it. What am I doing wrong.


Solution

  • According to the documentation:

    This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.

    and continuing:

    Important: You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.

    You need to implement one of those methods because dequeueReusableCellWithReuseIdentifier: forIndexPath: may need to create an object and needs the above methods to do so. Maybe you have created those and did not show them. If you have implemented one of them can you show your code?

    UPDATE:

    Based on your viewDidLoad and the error message you are getting it looks like CellIdentifier is an NSString. When you are calling [CellIdentifier class] you are registering a string as the use class, not a cell. You need to put the class you are using here. Based on your implementation it appears you would use [itemViewCell class].

    Just so you know, you should always capitalize classes. So if itemViewCell is an actual class you should change it to ItemViewCell and property and variable names should be lower case (so cellIdentifer vs CellIdentifier).