virindh virindh - 3 months ago 11
iOS Question

UICollectionViewCells not appearing in a UICollectionView

As the title suggests I have a

UICollectionView
that is hooked up to a
UIViewController
through IB. I have a
UICollectionView
subclass called
imageCell
. In the viewcontroller
viewDidLoad
method, I register the class like this:

CGRect collectionViewRect = CGRectMake(384, 60, 728, 924);
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
imageCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:flow];
[imageCollectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"Cell"];
imageCollectionView.delegate = self;
imageCollectionView.dataSource = self;


I then call a method called
getPhotos
in
viewDidAppear
(for UI reasons) that gets photos from a service and then I call
[imagCcollectionView reloadData];
inside
getPhotos
.

Then in
cellForItemAtIndexPath
I do the following:

imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
GRKAlbum *album = [albums objectAtIndex:indexPath.item];
cell.titleLabel.text = album.name;
return cell;


I know I am not doing anything wrong here because the method is being called and
cell
is not
nil
and neither is
album
.

So, when I run all of this code, the
UICollectionView
appears, but nothing is in it. All I can see is the background color of the
collectionView
. Any help at all would be greatly appreciated.

Note: I did NOT create an XIB for the
imageCell
class when I made it and I am using Storyboards.

Answer

There are some ways to debug this situation.

  1. Register UICollectionViewCell class instead of your customCell and set the cell's background color in cellForItem...

  2. If the cells are appearing in step one try to use your own cell class and set its background

Are you using storyboard? then..

You dont have to register any cell for the collection view. You can use the prototype cell, just give the proper id for the cell. No need to create custom class for the cell. Design everything in your prototype cell.