1

Why aren't my subclassed cells displaying?

I've tried calling awake From Nib and initWithCoder and initWithFrame but none of the attributes are displaying

 @interface PhotoCollectionViewCell : UICollectionViewCell
 @property (strong, nonatomic) IBOutlet UIImageView *photoImageView;
 @end

And in my .m file

 @implementation PhotoCollectionViewCell
 -(void)awakeFromNib {
  self.photoImageView.frame = self.bounds;
  self.backgroundColor = [UIColor lightGrayColor];
 }

 -(id)initWithCoder:(NSCoder *)aDecoder {
  self = [super initWithCoder:aDecoder];
  if (self) {
    self.photoImageView.frame = self.bounds;
    self.backgroundColor = [UIColor lightGrayColor];
  }
  return self;
 }

My cell's class in interfacebuilder is set to PhotoCollectionViewCell and the reuse identifer is properly set in interfacebuilder

 @interface HomeViewController () {
   NSMutableArray *photosArray;
 }
 @end

And homeview.m file

 -(void)viewDidLoad {
   [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"photoCell"];
   photosArray = [[NSMutableArray alloc] init];
   UIImage *placeholder = [UIImage imageNamed:@"placeholder.png"];
  [photosArray addObject:placeholder];
 }

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   return photosArray.count;
 }

 - (PhotoCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    cell.photoImageView.image = [photosArray objectAtIndex:indexPath.row];
    return cell;
 }

des

dfa

df

1
  • You don't need to register the class or the nib. That's what you're doing already in the storyboard. By registering the class you are overriding the storyboard and you will get your cell but with none of the outlets connected. Remove the line registerClass... Commented Sep 6, 2015 at 20:16

2 Answers 2

1

You don't need to register the class or the nib. That's what you're doing already in the storyboard. By registering the class you are overriding the storyboard and you will get your cell but with none of the outlets connected.

Remove the line...

[self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"photoCell"];

You don't need it because you're doing it in the Storyboard.

Sign up to request clarification or add additional context in comments.

1 Comment

beautiful! thank you. I actually did read the apple documentation on it and that wasn't annotated in there at all. I would have spent hours without even trying that. And unfortunately all the tutorials i looked at had that in their tutorial even if they were using storyboard. downfall of trusting tutorials i guess. thanks
-1

In homeview.m you should register the NIB not the class.

UINib *cellnib = [UINib nibWithNibName:PhotoCollectionViewCell bundle:nil];
[self.collectionView registerNib:cellnib forCellReuseIdentifier:@"photoCell"];

1 Comment

i'm not using a nib and it's not a tableView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.