【发布时间】:2014-08-08 22:45:36
【问题描述】:
我通过代码添加了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;
}
【问题讨论】:
-
为什么在初始化之前使用self.collectionView?
标签: ios uicollectionview