只是做一下记录,以后懒得再写了 。
在viewDidload 里面添加并且注册 cell ,header,footer 。注意header 和footer 自定义的时候 新建一个view 继承 UICollectionReusableView 。
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[YMHomeCell class] forCellWithReuseIdentifier:@"GoodsCell"];
[self.collectionView registerClass:[YMHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView"];
[self.collectionView registerClass:[YMHomeFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterReusableView"];
具体设置
#pragma mark ------------------ 懒加载
-(UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUSHEIGHT - NAVGATIONHEIGHT - TABBARHEIGHT) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.showsVerticalScrollIndicator = NO;
}
return _collectionView;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return15;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return10;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
YMHomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsCell" forIndexPath:indexPath];
returncell;
}
//设置头部
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//header
YMHomeHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderReusableView" forIndexPath:indexPath];
returnheader;
}else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//footer
YMHomeFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterReusableView" forIndexPath:indexPath];
returnfooter;
}
return [UICollectionReusableView new];
}
-(CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section{
if(section ==0) {
return CGSizeMake(0,0);
}else{
returnCGSizeMake(SCREEN_WIDTH,45);
}
}
-(CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForFooterInSection:(NSInteger)section{
returnCGSizeMake(0,0);
}
- (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath*)indexPath{
returnCGSizeMake(SCREEN_WIDTH/2,200);
}
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section{
return8;
}
- (CGFloat)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return0;
}
最后的结果大概是这个样子

网友评论