2013-04-02 24 views
8

Tôi đang tạo một UICollectionViewController theo chương trình. Không, tôi không muốn sử dụng IB và Storyboards như mọi hướng dẫn trên mạng. Tôi chỉ muốn sử dụng mã đơn giản.UICollectionViewController Lập trình bị lỗi với lỗi tham số bố cục phi nil

Đây là những gì tôi có trong tôi 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]; 

tôi đã thực hiện các phương pháp đại biểu cần thiết và tôi vẫn nhận được lỗi này:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 

Trả lời

10

Đây là những gì tôi sử dụng để khởi tạo một UICollectionViewController :

- (id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout 

Bạn không cần phải instantia te bộ sưu tậpXem bộ điều khiển sẽ làm điều đó cho bạn. Đặt khung sau khi gọi điện thoại này hoặc bạn có thể ghi đè và đặt khung bên trong:

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

Tôi thích sử dụng các ràng buộc autolayout.

Các vấn đề liên quan