Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCells not appearing in a UICollectionView

Tags:

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.

like image 276
virindh Avatar asked Apr 18 '13 01:04

virindh


2 Answers

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.You can design everything in your prototype cell. Custom class may not be needed in all the cases.

like image 182
Anil Varghese Avatar answered Sep 17 '22 15:09

Anil Varghese


If you created the cell in the storyboard, then just remove the register class statement. You don't need to register anything (for a collection view or table view) if you make the cell in a storyboard (in fact, it screws things up if you do so). The only thing you need to do is make sure that you set the identifier in IB.

like image 20
rdelmar Avatar answered Sep 18 '22 15:09

rdelmar