//创建布局
UICollectionViewFlowLayout * danPinFlowLayout =[[UICollectionViewFlowLayout alloc]init];
danPinFlowLayout.itemSize= CGSizeMake(230, 240);
danPinFlowLayout.headerReferenceSize=CGSizeMake(0, 50);
danPinFlowLayout.footerReferenceSize =CGSizeMake(0, 50);
danPinFlowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;//竖向滚动
//初始化collectionView
CGRect collectRect=CGRectMake(0, 50, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
if(_DanPinCollectionView ==nil){
_DanPinCollectionView =[[UICollectionView alloc]initWithFrame:collectRect collectionViewLayout:danPinFlowLayout];
_DanPinCollectionView.tag=200;
_DanPinCollectionView.backgroundColor=[UIColor whiteColor];
_DanPinCollectionView.dataSource=self;
_DanPinCollectionView.delegate=self;
_DanPinCollectionView.showsVerticalScrollIndicator = NO;
[self.view addSubview:_DanPinCollectionView];
//注册cell
UINib *nibCell = [UINib nibWithNibName:@"DanPinCollectionViewCell"
bundle: [NSBundle mainBundle]];
[_DanPinCollectionView registerNib:nibCell forCellWithReuseIdentifier:@"danPinCellIdentifier"];
//headerView注册
[_DanPinCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderIdentifierhead"];
//footView注册
[_DanPinCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FootIdentifierhead"];
}
}
cell模板建议使用xib文件直接创建,更快捷方便。
基础协议实现
#pragma mark ----------------UIcollectionViewDataSource---------------
//组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//单元格的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;//self.DanPinDataSource.count;
}
//构建单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"danPinCellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
//组的头视图创建
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if(kind ==UICollectionElementKindSectionHeader){
UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderIdentifierhead" forIndexPath:indexPath];
headView.backgroundColor = [UIColor blueColor];
return headView;
}
else{
UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FootIdentifierhead" forIndexPath:indexPath];
footView.backgroundColor = [UIColor redColor];
return footView;
}
}
//通过协议方法设置单元格尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// CGFloat rd = arc4random()%200;
return CGSizeMake(220, 240);
}