Instead of UICollectionView a black screen is displayed
Asked Answered
F

6

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.

Fruiter answered 31/3, 2015 at 10:37 Comment(0)
E
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!

Experiential answered 31/3, 2015 at 12:7 Comment(0)
M
6

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

Mohamedmohammad answered 27/7, 2016 at 11:34 Comment(1)
if you want the background to be clear, you have to use collectionView.backgroundColor = [UIColor clearColor]; and collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];Iatry
E
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]];
Electroencephalogram answered 24/2, 2016 at 4:47 Comment(0)
D
2
[self.collectionView registerNib:[UINib nibWithNibName:@"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ProductCollectionViewCell"];

self.collectionView.backgroundColor = [UIColor clearColor];
Dozen answered 13/10, 2016 at 5:45 Comment(0)
S
2

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

Scheel answered 14/10, 2017 at 17:55 Comment(0)
F
1

In Swift,

self.label.textColor = UIColor.whiteColor()
Fireplace answered 28/7, 2016 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.