我UICollectionView
通过代码添加。
现在,应用程序崩溃并显示以下消息:UICollectionView must be initialized with a non-nil layout parameter
。
您有解决的主意吗?
CollectionCell
是的自定义类UICollectionViewCell
。
@property (nonatomic, strong) UICollectionView* collectionView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionView = [[UICollectionView alloc]init];
[self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"cell"];
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(100, 100);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collectionView setCollectionViewLayout:flowLayout];
self.collectionView.frame = CGRectMake(0, 60, 320, 500);
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.eventCollection];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionCell* cell = [self.eventCollection dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%d", indexPath.item];
return cell;
}