2013-04-02 48 views
8

我正在以編程方式創建UICollectionViewController。不,我不想使用IB和Storyboard,比如每個教程。我只想使用簡單的代碼。UICollectionViewController以非零布局參數錯誤編程崩潰

以下是我在我的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' 

回答

10

這是我用來初始化UICollectionViewController

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout 

你不需要instantia收集查看控制器將爲您做。設置框架調用此之後還是可以在裏面覆蓋,並設置框:

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{ 
    self = [super initWithCollectionViewLayout:layout]; 
    if (self) { 
     // additional setup here if required. 
    } 
    return self; 
} 

我更喜歡使用autolayout約束。

相關問題