Ios UICollectionViewController与流布局崩溃
我刚刚通过编程创建了一个UICollectionViewController。到目前为止一切都很好,但我面临一些问题。调用CollectionViewController时,我的应用程序立即崩溃,返回此错误 “UICollectionView必须使用非nil布局参数初始化” 当然,我试图插入UICollectionViewFlowLayout,但它继续崩溃,我不明白为什么。有人能给我解释一下如何在不使用情节提要的情况下使用UICollectionViewController吗 我哪里做错了Ios UICollectionViewController与流布局崩溃,ios,objective-c,uicollectionview,uicollectionviewlayout,uicollectionviewflowlayout,Ios,Objective C,Uicollectionview,Uicollectionviewlayout,Uicollectionviewflowlayout,我刚刚通过编程创建了一个UICollectionViewController。到目前为止一切都很好,但我面临一些问题。调用CollectionViewController时,我的应用程序立即崩溃,返回此错误 “UICollectionView必须使用非nil布局参数初始化” 当然,我试图插入UICollectionViewFlowLayout,但它继续崩溃,我不明白为什么。有人能给我解释一下如何在不使用情节提要的情况下使用UICollectionViewController吗 我哪里做错了 @i
@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;
}
解决了我的问题。。。谢谢大家在这篇文章中给了我很多提示和建议。。你真是太好了
所有这些问题都是因为UICollectionViewController在显示时未使用其布局进行初始化
我使用[self-presentViewController:collectionViewController animated:YES completion:nil]方法从另一个UIViewController调用UICollectionViewController。。。。此时问题出现了
UICollectionViewController仅分配用于显示,没有显示所需的布局。。。这就是为什么这该死的碰撞
所以我就这样解决了
UIViewController.m
UICollectionViewController.m
在这一点上,一切都正常
在这种情况下,在UICollectionViewController内部,您不需要指定任何类型的布局,因为它是在类呈现之前的类分配过程中指定的如果您正在使用UICollectionViewController或已将其子类化,则已经为其分配了UICollectionView实例。您不需要实例化或创建新的。我怀疑这不起作用的原因是,即使将新视图指定给self.collectionView,也没有将其添加到层次结构中 而不是self.collectionView=[[UICollectionView alloc]init 只需将布局分配给现有UICollectionView,例如:
self.collectionView.collectionViewLayout=layout如何初始化collectionView?@ReinierMelian不初始化对象就不能调用方法。Sh_Khan的答案是唯一正确的方法。你的UICollectionView是来自情节提要还是以编程方式创建的?@AlejandroIván没有情节提要o您的视图控制器实际上是UICollectionViewController的子类?将其作为UIViewController的子类,因为UICollectionViewController已经自动嵌入了UICollectionView。
-(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];
}
-(void)viewDidLoad {
[super viewDidLoad];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}