iOS-UI之UICollectionView must be initialized with a non-nil layout parameter'解决方式

出现错误:


解决方式:



用xib创建的集合视图

#import "ViewController.h"

#import "CollectionCell.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width-10*2)/3, 300);

    flowLayout.minimumInteritemSpacing = 10;

    flowLayout.minimumLineSpacing = 20;

    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    

    UICollectionView *collView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];

    collView.delegate = self;

    collView.dataSource = self;

    

    [collView registerNib:[UINib nibWithNibName:@"CollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:collView];

    

    [collView reloadData];

    

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    

    return 60;

}


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

//    cell.backgroundColor = [UIColor blueColor];

    

    return cell;

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(iOS-UI之UICollectionView must be initialized with a non-nil layout parameter'解决方式)