而不是UICollectionView显示黑屏

tes*_*ing 24 objective-c interface-builder ios infinite-scroll uicollectionview

我正在尝试重新实现这里UICollectionView看到的不定式滚动.对我来说遗失的东西:

ViewController.h:

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

DataCell.h:

@interface DataCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *label;
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

CustomCollectionView.h:

@interface CustomCollectionView : UICollectionView

@end
Run Code Online (Sandbox Code Playgroud)

对于整个项目,我使用了故事板和法线UIViewController.在这个视图控制器上,我UICollectionView在Interface Builder中添加了一个.我将集合视图中的插座与视图控制器连接起来,并设置数据源并再次将方法委托给我的视图控制器.我还在UICollectionViewCellInterface Builder中设置了自定义类和重用标识符.

所以一切都应该工作,但我只得到一个黑屏.我错过了什么?您可以在此处下载整个项目.

Jav*_*ont 24

您正在正确配置CollectionView,只是忘记了标签的颜色:)

    [self.label setTextColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

希望能帮助到你!


Sol*_*uiD 5

您需要在故事板中手动设置集合视图的背景颜色.

默认情况下它是黑色的(尽管在故事板编辑器中没有显示)

在此输入图像描述