我正在创建一个UICollectionViewController
以编程方式。不,我不想像每个教程一样使用 IB 和故事板。我只想使用纯代码。
这是我的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];
我已经实现了所需的委托方法,但仍然收到此错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
这就是我用来初始化的UICollectionViewController:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
您不需要实例化 collectionView,控制器会为您完成此操作。调用此函数后设置框架,或者您可以覆盖并设置内部框架:
- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
self = [super initWithCollectionViewLayout:layout];
if (self) {
// additional setup here if required.
}
return self;
}
我更喜欢使用自动布局限制。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)