2014-05-14 232 views
0

我试图用自动布局将UICollectionView添加到我的应用程序,但它不断崩溃。这是我的代码:iOS CollectionView崩溃

_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' 
+0

为崩溃表明我猜这个问题可能在_collection.collectionViewLayout = [DXSelectionViewLayout new]; – user2071152

回答

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]; 
0

我认为你可以找到有你的答案:

Creating a UICollectionView programmatically

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

所以一个集合不能使用自动布局? – Haagenti

+0

@MouNtant您仍然可以使用自动布局来定位您的收藏视图。如果你这样做的话,你可以通过一个近似大小的CGRect作为框架。 –