UICollectionView 设置头标题和头标题的高度

#pragma mark - 设置 CollectionView
/** 设置 CollectionView */
-(void)setUpCollectionView{
    
    self.backgroundColor = [UIColor colorFromHexRGB:@"EEEEEE"];
    
    /** 左右间距 */
    CGFloat lR = (self.width - floor(self.width / 3) * 3) / 2;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.width - 2 * lR , self.frame.size.height - 64 - 49) collectionViewLayout:layout];
    [self addSubview:self.collectionView];
    
    /** 设置代理和数据源 */
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    /** 注册 cell */
    [self.collectionView registerClass:[DYLiveCategoryCell class] forCellWithReuseIdentifier:liveID];
    [self.collectionView registerClass:[DYRecentLiveCategoryCell class] forCellWithReuseIdentifier:recentID];
    [self.collectionView registerClass:[DYLiveCategoryHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:DYLiveCategoryHeaderViewID];
    self.collectionView.backgroundColor = [UIColor colorFromHexRGB:@"EEEEEE"];// QGColorWithHexString(@"efefef");
}



#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.liveCategorys.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DYRecentLiveCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:recentID forIndexPath:indexPath];
        cell.recentCategoryDelegate = self;
        cell.recentLiveCategoryModels = [DYLiveCategoryTool recentCategorys];
        return cell;
    }
    
    DYLiveCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:liveID forIndexPath:indexPath];
    SubColumnModel *liveCategoryModel = self.liveCategorys[indexPath.row];
    cell.liveCategoryModel = liveCategoryModel;
    return cell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(self.frame.size.width, 105);
    }
    
    return CGSizeMake(floor(self.collectionView.frame.size.width / 3), floor(self.collectionView.frame.size.width / 3));
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    /** 点击 cell 后取消选中 */
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    /** 取出选中的数据 */
    SubColumnModel *liveCategoryModel = self.liveCategorys[indexPath.row];
    
    /** 存储到最近使用 */
    [DYLiveCategoryTool addRecentCategory:liveCategoryModel];
    
    /** 取出第一个分区的 cell */
    DYRecentLiveCategoryCell *cell = (DYRecentLiveCategoryCell * )[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    /** 重新赋值 */
    cell.recentLiveCategoryModels = [DYLiveCategoryTool recentCategorys];
    
    /** 返回到顶部的位置 */
    [cell.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    [self pushWitModel:liveCategoryModel];
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(self.frame.size.width, 45);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        DYLiveCategoryHeaderView *headView  = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:DYLiveCategoryHeaderViewID forIndexPath:indexPath];
        headView.backgroundColor            = [UIColor whiteColor];
        headView.model                      = self.liveHeaders[indexPath.section];
        return headView;
        
    }
    return nil;
}




-(void)DYRecentLiveCategoryCellDidSelectItemAtIndexPath:(NSIndexPath *)indexPath collectionView:(UICollectionView *)collectionView{
    /** 点击 cell 后取消选中 */
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    /** 取出选中的数据 */
    SubColumnModel *liveCategoryModel =[DYLiveCategoryTool recentCategorys][indexPath.row];
    
    /** 存储到最近使用 */
    [DYLiveCategoryTool addRecentCategory:liveCategoryModel];
    
    /** 取出第一个分区的 cell */
    DYRecentLiveCategoryCell *cell = (DYRecentLiveCategoryCell * )[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    /** 重新赋值 */
    cell.recentLiveCategoryModels = [DYLiveCategoryTool recentCategorys];
    
    /** 返回到顶部的位置 */
    [cell.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    [self pushWitModel:liveCategoryModel];
}

-(void)pushWitModel:(SubColumnModel* )model{
    DYClassifyLiveViewController *classifyVC = [[DYClassifyLiveViewController alloc] init];
    classifyVC.catId    = model.tag_id;
    classifyVC.tagId    = @"-1";
    classifyVC.catName  = model.tag_name;
    [[ControllerTool getCurrentVC] pushViewController:classifyVC animated:YES];
}
/** 设置数据源 */
-(void)setCategorys:(NSMutableArray *)categorys{
    _liveCategorys = categorys;
    [self.collectionView reloadData];
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值