objective-C CollectionView 加深(添加注册头部View)

#pragma marc 添加Collection
-(void)addConllectionView{
    //collection的布局方案
    UICollectionViewFlowLayout *collectionViewLayout=[[UICollectionViewFlowLayout alloc]init];
    //设置位置大小以及布局方案
    _myCollectionView=[[UICollectionView alloc]initWithFrame:CM(0, 0, VIEW_WIDTH,VIEW_HEIGHT-64-48) collectionViewLayout:collectionViewLayout];
    //CollectionView的背景颜色
    _myCollectionView.backgroundColor=[UIColor whiteColor];
     [self addSubview:_myCollectionView];
    //设置代理
    _myCollectionView.delegate=self;
    _myCollectionView.dataSource=self;
    //注册Cell 以及选择控制的类
    [_myCollectionView registerClass:[ExhibitionCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册Head以及Food 以及选择控制的类
    [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head"];

}
#pragma mark 设置Cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ExhibitionCollectionViewCell *cell=[_myCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];
    cell.imageView.image=[UIImage imageNamed:@"商品"];
    return cell;
}
#pragma mark 设置头部以及尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    //和判断复用一样
        UICollectionReusableView * head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head" forIndexPath:indexPath];
        //添加轮播图
        [head addSubview:_carouselFigureView];
        return head;
    }
    return nil;
    
}
#pragma mark 设置Cell返回数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}
#pragma mark 定义展示的组数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark 设置cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   return CGSizeMake(VIEW_WIDTH/3.55f, VIEW_HEIGHT/3.550f);
    
}

#pragma mark 设置每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
#pragma mark 设置每个section中不同的列之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
#pragma mark 设置每个Cell的内边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 10, 0);//分别为上、左、下、右
}
#pragma mark 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CommodityDisplayControllerView * CDVC=[[CommodityDisplayControllerView alloc]init];
    [[self getController] presentViewController:CDVC animated:YES completion:nil];
    NSLog(@"%ld",indexPath.row);
}

#pragma mark 返回头headerView的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
   CGSize size={VIEW_WIDTH,IMG_EXHIBITION_HEIGHT};
    return size;
}


你可能感兴趣的:(objective-C CollectionView 加深(添加注册头部View))