23

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.

BytesGuy
  • 4,097
  • 6
  • 36
  • 55
virindh
  • 3,775
  • 3
  • 24
  • 49

8 Answers8

88

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.

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • 13
    thanks a lot.. it's very useful solution. i just removed [self.collectionView registerClass:[cutomCollectionCell class] forCellWithReuseIdentifier:@"cutomCollectionCell"]; and wow, it's working fine. – kalpesh jetani Apr 26 '14 at 10:09
  • 6
    This problem had bothered me for half a day. Can't believe it is so simple to solve. I hope next version of XCode would at least show some warnings if they think registerClass is unnecessary or misbehaviour for storyboard. – newguy Apr 27 '14 at 03:20
  • 4
    Removing `registerClass` part helped me too. Thanks a lot! – Maksim Apr 26 '15 at 18:49
  • There's an old saying in China: teach a man how to fish rather than giving him the fish :) – addlistener Jan 31 '16 at 15:37
  • Remember to implement the numberOfSectionsInCollectionView and collectionView methods if you are also using a custom CollectionViewController class. – emily May 24 '16 at 13:40
  • Can you elaborate on "no need to create custom class for the cell"? I'm trying to replicate an iMessage like collection view but idk how you'd edit each individual bubble using only the prototype – Script Kitty Mar 14 '17 at 04:09
  • 1
    @ScriptKitty ya my statement is little confusing. Custom class is not need in all cases like if you are doing as prototype cell in storyboard and you dont have much to customise. Its good to have a custom class for better accessing the cell properties else you could access via `viewWithTag` – Anil Varghese Mar 14 '17 at 06:41
  • @AnilVarghese, bro could you help me to take a look at my problem https://stackoverflow.com/questions/45074376/uicollection-view-inside-table-cell-does-not-show-images-in-swift-3 ? – May Phyu Jul 13 '17 at 08:58
45

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • RegisterClass: screwed me though. :( :) – Dinesh Raja Apr 16 '15 at 06:56
  • 1
    This fixed it for me. Anyone knowledgeable enough to know why it not only doesn't work like this, but completely screws it up? – micnguyen Oct 16 '15 at 03:35
  • you're always my hero, @rdelmar! I actually had no idea about this. I happened to leave in the now default example code that has the "register" thing - I've only ever used cells made naturally in the storyboard - so I had no clue what was going on. Thanks for the save! – Fattie Jun 25 '16 at 19:57
22

I had the same issue fixed with replacing conventional registercell line with below line

[self.collection registerNib:[UINib nibWithNibName:@"nibname" bundle:nil] forCellWithReuseIdentifier:identifier];

Swift 4.2 version: (thanks Nikolay Suvandzhiev)

self.collection.register(UINib(nibName: "nibname", bundle: nil), forCellWithReuseIdentifier: identifier)
BhushanVU
  • 3,455
  • 1
  • 24
  • 33
  • 2
    This helped me ! If you have a XIB for your cell then use this method. I was using auto complete and I landed up using registerClass:forCellWithReuseId: and that caused my views to not load and hence not show up. – RPM Sep 07 '16 at 06:41
  • Thanks, this worked! Swift 4.2 version: `self.collection.register(UINib(nibName: "nibname", bundle: nil), forCellWithReuseIdentifier: identifier)` – Nikolay Suvandzhiev Nov 25 '18 at 18:38
3

The mistake is the way how you register the Nib/Xib file (using Swift 5):

use this register method:

myCollectionView.register(UINib(nibName: "myCustomCell", bundle: nil), forCellWithReuseIdentifier: "myCustomCell")

instead of this:

myCollectionView.register(myCustomCell.self, forCellWithReuseIdentifier: "myCustomCell")
Osama Remlawi
  • 2,356
  • 20
  • 21
1

I felt like a dummy when I realized my collection view cells were being covered up by my navigation bar. Sooo, make sure your navigation bar (if you have one) isn't covering the collection view cells you're testing with.

nenchev
  • 1,998
  • 28
  • 16
0

You're instantiating a new collection view in your code, but aren't adding the view to the screen. You say you're already using storyboards, my guess is that what you can see is the collection view from your storyboard, which presumably isn't connected to a datasource or delegate? Either that or you're not calling reload on the right collection view.

Either configure your collection view in the storyboard (and set up an outlet to it for anything that must be done in code) or set it up completely in code. At the moment I think you have two collection views.

jrturton
  • 118,105
  • 32
  • 252
  • 268
0

I made it using Xcode 6.2 storyboard. Hope it helps

#import <UIKit/UIKit.h>

@interface InfoSecurity : UIViewController
@property (strong, nonatomic) IBOutlet UICollectionView *tabsCV;

@end

.m
**************

#import "InfoSecurity.h"
#import "TabsCell.h"

@interface InfoSecurity ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation InfoSecurity

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.tabsCV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"TabsCell"];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 03;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 01;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(90, 50);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier = @"tabscellID";


    TabsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    cell.TabTitleLabel.text= @"OPTIONS";

    return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
alvin
  • 1
0

Sometimes the collection view cell is black (same color with background, invisible, hidden is no), reloading the visible cells is a work around.

Yao Li
  • 2,071
  • 1
  • 25
  • 24