代码添加UICollectionView添加头视图

Demo 地址 :https://github.com/MoreZone/UIcollectionHeadDemo.git

往期瀑布流 地址:https://github.com/MoreZone/CLWaterFlowDemo.git 


最近用到UICollectView 头视图,不同于tableview 的头视图一句话完事 。

1)tabview 用到头视图时候只需要一句话就能用  

 self.tableView.tableHeaderView = [[UIView alloc]init];

相关代理方法

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

}

2)用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController,使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议
跟tableview差不多的用法

二  具体方法
创建UICollectionView前 需要设置布局

    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayoutalloc]init];

    layout.sectionInset =UIEdgeInsetsMake(0,0,0, 0);

    layout.headerReferenceSize =CGSizeMake(YJTViewWidth,50*YJTRatioH);//头视图大小


// 注册头视图

 

[_collectionregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"];


相关的代理调用

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{


    UICollectionReusableView *header = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"header"forIndexPath:indexPath];

    

    header.backgroundColor = RGB(236, 237,241);

    

    if (indexPath.section ==0) {

        labelOne.text =@"热门检查";

        labelOne.font = [UIFontsystemFontOfSize:14.0f];

        labelOne.textColor =MainRGB;

        [header addSubview:labelOne];

        

    }else{

        

        labelTwo.text =@"疾病信息";

        labelTwo.font = [UIFontsystemFontOfSize:14.0f];

        labelTwo.textColor =MainRGB;

        [header addSubview:labelTwo];

    }

    return header;

}












你可能感兴趣的:(代码添加UICollectionView添加头视图)