Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

chitra iyer
chitra iyer
3,722 Points

PhotoBombers ReuseIdentifier

When I register the class in viewDidLoad() it does not get registered and I get the runtime error - could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier .... So I registered the class in viewWillAppear() and it works fine. Just wondering if anyone encountered this problem? And if registering it in the viewWillAppear() is fine?

-(void)viewDidLoad:(BOOL)animated{

    [super viewDidLoad];

}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"photo"];

}

4 Answers

Stone Preston
Stone Preston
42,016 Points

it should probably go in viewDidLoad. also note that if you have a custom collection view cell class you need to be using the name of your class, not UICollectionViewCell

self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[SPPhotoCell class] forCellWithReuseIdentifier:@"photo"];
chitra iyer
chitra iyer
3,722 Points

I agree it should go in viewDidLoad but its not working there.

As for the class name I am in the second video now and have pointed it to the custom class, but that does not have anything to do with my Q, i hope.

Stone Preston
Stone Preston
42,016 Points

thats weird that it wouldnt work

chitra iyer
chitra iyer
3,722 Points

I get the runtime error - could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier ....

Stone Preston
Stone Preston
42,016 Points

Make sure you are using the same reuse identifier in cellForItemAtIndexPath. You need to use "photo" as the reuse identfier