Reputation: 3765
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.
Upvotes: 23
Views: 38322
Reputation: 2990
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")
Upvotes: 4
Reputation: 3455
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)
Upvotes: 22
Reputation: 42977
There are some ways to debug this situation.
Register UICollectionViewCell class instead of your customCell and set the cell's background color in cellForItem...
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.
Upvotes: 88
Reputation: 2256
Sometimes the collection view cell is black (same color with background, invisible, hidden is no), reloading the visible cells is a work around.
Upvotes: 0
Reputation: 2067
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.
Upvotes: 1
Reputation: 1
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.
}
Upvotes: 0
Reputation: 119242
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.
Upvotes: 0
Reputation: 104082
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.
Upvotes: 45