I've just created a UICollectionViewController
programatically. Up to here everything is fine but I'm facing some problems. My app immediately crashes when the CollectionViewController is called, returning this error.
'UICollectionView must be initialized with a non-nil layout parameter'
I tried to insert UICollectionViewFlowLayout
of course, but it continues to crash and I do not understand why. Can someone explain to me how to use a UICollectionViewController
without using the storyboard ??
Where am I doing wrong?
@implementation ChooseRegion
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
layout.itemSize = CGSizeMake(100, 100);
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
return 0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
return cell;
}
Solved my problem ... Thank you all for the many tips and suggestions you gave me in this .. you were very kind !!
all this problem occurs because the UICollectionViewController
is not initialized with its layout at the time of its presentation.
I call UICollectionViewController
from another UIViewController
using the [self presentViewController:collectionViewController animated:YES completion:nil]
method .... At this point the problem occurs ...
The UICollectionViewController
was only allocated to be presented and did not have the necessary layout to be shown ... that's why this damn crash !!!
So I solved this way
UIViewController.m
-(void)presentCollectionViewController {
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
layout.itemSize = CGSizeMake (100, 100);
[layout setScrollDirection: UICollectionViewScrollDirectionHorizontal];
UICollectionViewController * chooseRegion = [[UICollectionViewController alloc] initWithCollectionViewLayout:layout];
[self presentViewController: chooseRegion animated: YES completion: nil];
}
UICollectionViewController.m
-(void)viewDidLoad {
[super viewDidLoad];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
At this point everything works ...
Inside the UICollectionViewController you do not need to specify any type of layout in this case, as it is specified during the allocation of the class before its presentation