本文介绍了UICollectionViewController程序崩溃与非nil布局参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以编程方式创建了一个 UICollectionViewController
。不,我不想使用IB和故事板像每一个教程。
I am creating a UICollectionViewController
programmatically. No, I don't want to use IB and Storyboards like every tutorial out there. I just want to use plain code.
这里是我在 viewDidLoad
中的:
// UICollectionView
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setItemSize:CGSizeMake(200, 140)];
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:aFlowLayout];
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
self.collectionView.backgroundColor = [UIColor clearColor];
[self.collectionView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth];
[self.collectionView registerClass:[PUCImageGridCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
我实现了所需的委托方法,我仍然得到这个错误:
I have implemented the required delegate methods and I still get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
推荐答案
这是我用来初始化:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
您不需要实例化collectionView控制器会为你做它。在调用此框架之后设置框架,或者您可以覆盖框架并设置框架:
You do not need to instantiate the collectionView the controller will do it for you. Set the frame after calling this or you can override and set the frame inside:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
self = [super initWithCollectionViewLayout:layout];
if (self) {
// additional setup here if required.
}
return self;
}
我更喜欢使用约束。
这篇关于UICollectionViewController程序崩溃与非nil布局参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!