2014-05-14 146 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作爲框架。 –