Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView: must be initialized with a non-nil layout parameter

I added UICollectionView by code.
Now, the app crash with message: UICollectionView must be initialized with a non-nil layout parameter.
Do you have any idea to fix it?
CollectionCell is custom class of 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; } 
like image 902
kusumoto_teruya Avatar asked Jun 18 '14 15:06

kusumoto_teruya


People also ask

What is swift Uicollectionview?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is Collectionviewlayout?

An abstract base class for generating layout information for a collection view.


1 Answers

The crash is telling you pretty explicitly what is going wrong:

UICollectionView must be initialized with a non-nil layout parameter.

If you check the documentation for UICollectionView, you'll find that the only initializer is initWithFrame:collectionViewLayout:. Further, in the parameters for that initializer, you'll see:

frame

The frame rectangle for the collection view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This frame is passed to the superclass during initialization.

layout

The layout object to use for organizing items. The collection view stores a strong reference to the specified object. Must not be nil.

I've bolded the important part. You must use initWithFrame:collectionViewLayout: to initialize your UICollectionView, and you must pass it a non-nil UICollectionViewLayout object.


One way to fix this, then, would be to simply change the order of initialization you do:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(100, 100); [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; 

Note that in the above example, I've assumed you want to use self.view.frame as the frame of self.collectionView. If that's not the case, insert whatever frame you want instead.

like image 89
ravron Avatar answered Oct 04 '22 21:10

ravron