三个协议都在头文件里面实现了,但是运行的时候 他仍然提示 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'不知道问题出在哪里。layout明明创建并给 self . collectionView赋值了还是说为空。
目前处于自学阶段。。求学习资源#import "RootViewController.h"
 
 
@implementation RootViewController
 
 
- (void)viewDidLoad {
 
    self.title = @"集合视图";
 
     
 
    UICollectionViewLayout *layout = [[UICollectionViewLayout alloc]init];
 
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
 
     
 
    self.collectionView.delegate = self;
 
    self.collectionView.dataSource = self;
 
 
     
 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
 
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell"];
 
     
 
    [self.view addSubview:self.collectionView];
 
}
 
 
#pragma dataSource
 
 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
 
    return 4;
 
}
 
 
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
 
    return 5;
 
}
 
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
 
    return cell;
 
}
 
 
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
 
    UICollectionReusableView *headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell" forIndexPath:indexPath];
 
    return headerCell;
 
}
 
 
#pragma delegateLayout
 
 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
 
    return CGSizeMake(0, 30);
 
}
 
 
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 
    return CGSizeMake(self.view.frame.size.width/3.0, 100);
 
}
 
 
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
 
    return UIEdgeInsetsMake(0, 0, 0, 0);
 
}
 
 
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
 
    return 0;
 
}
 
 
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
 
    return 0;
 
}
 
 
#pragma UICollectionViewDelegate
 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
    NSLog(@"%zi组,%zi行",indexPath.section,indexPath.item);
 
}