26

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.

Community
  • 1
  • 1
testing
  • 17,950
  • 38
  • 208
  • 373

6 Answers6

25

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!

Javier Flores Font
  • 1,995
  • 13
  • 13
5

You need to manually set the background color of the collection view in the storyboard.

By default it is black (although not showing that in the storyboard editor)

enter image description here

SoliQuiD
  • 1,717
  • 18
  • 26
  • if you want the background to be clear, you have to use `collectionView.backgroundColor = [UIColor clearColor];` and `collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];` – Multinerd Dec 01 '16 at 21:11
4

I had the same issue. The black screen seems to be an indicator of no data available with collection view to display. Try changing the background color of the collection view, if that changed color got to display, your collection view is working. And then add some imageview to the collection view with tag (Ex. give a value 100 with the tag value for the image view) and with the cellforItemAtIndexPath set the images to the image view. (You can do this with custom cell. But for now, to get the collection view work, the assignment with tag for the imageview suits better)

UIImageView * ImageView = (UIImageView *)[cell viewWithTag:100];
ImageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
Tony
  • 53
  • 9
2

it happened to me that both the collectionView and the collection view cell had transparent backgrounds

Dan Precup
  • 75
  • 1
  • 4
1

In Swift,

self.label.textColor = UIColor.whiteColor()
Ghulam Rasool
  • 3,421
  • 2
  • 21
  • 36
1
[self.collectionView registerNib:[UINib nibWithNibName:@"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ProductCollectionViewCell"];

self.collectionView.backgroundColor = [UIColor clearColor];
testing
  • 17,950
  • 38
  • 208
  • 373
Mr.Javed Multani
  • 9,803
  • 2
  • 42
  • 45