iOS CollectionView崩溃

时间:2014-05-14 14:18:18

标签: ios uicollectionview

我正在尝试使用自动布局将UICollectionView添加到我的应用中,但它会一直崩溃。这是我的代码:

_collection = [[UICollectionView alloc] init];
[_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellId];
_collection.collectionViewLayout = [DXSelectionViewLayout new];
_collection.translatesAutoresizingMaskIntoConstraints = NO;
_collection.dataSource = self;
_collection.hidden = YES;

错误:

2014-05-14 16:16:09.978 Sportlinked[4712:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

2 个答案:

答案 0 :(得分:0)

我想你可以在那里找到答案:

Creating a UICollectionView programmatically

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];

答案 1 :(得分:0)

对于UICollectionView,您需要使用

- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout

作为初始化程序 - 而不是简单的

- (id)init

方法

(集合视图必须使用布局进行初始化。)

所以实际上用这样的代码替换你的第一行代码,以便正确地创建一个集合视图:

CGRect frame = CGRectMake(0, 0, 200, 200); // sample frame
UICollectionViewFlowLayout *layout= [UICollectionViewFlowLayout new]; // standard flow layout
_collection = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
相关问题