testing testing - 4 months ago 32
iOS Question

Instead of UICollectionView a black screen is displayed

I'm trying to reimplement the infinitive scrolling

UICollectionView
seen here. Things that were missing for me:

ViewController.h:

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

@end


DataCell.h:

@interface DataCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *label;
@end


DataCell.m:

#import "DataCell.h"

@implementation DataCell

-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
self.label = [[UILabel alloc] initWithFrame:self.bounds];
self.autoresizesSubviews = YES;
self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
self.label.textAlignment = NSTextAlignmentCenter;
self.label.adjustsFontSizeToFitWidth = YES;

[self addSubview:self.label];
}

return self;
}

@end


CustomCollectionView.h:

@interface CustomCollectionView : UICollectionView

@end


For the whole project I used a storyboard and a normal
UIViewController
. On this view controller I added a
UICollectionView
in Interface Builder. I connected the outlet from the collection view with my view controller and set up the datasource and delegate methods again to my view controller. I also set the custom class of the
UICollectionViewCell
and the reuse identifier in Interface Builder.

So everything should work but I only get a black screen. What I'm missing? You can download the whole project here.

Answer

You are configuring correctly the CollectionView, just that you forgot the color of the label :)

    [self.label setTextColor:[UIColor whiteColor]];

enter image description here

Hope it helps!