您的位置:首页 > 产品设计 > UI/UE

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

2016-07-22 20:52 483 查看
出现错误:



解决方式:



用xib创建的集合视图

#import "ViewController.h"

#import "CollectionCell.h"

@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@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;

    

}

<
aaf4
span style="font-size:18px;color:#666666;">- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息