纯代码添加UICollectionView的头视图,最近项目中用到这个虽然简单记录下供大家看看。
- UICollectionView和tableview都需要遵从代理和数据源的方法。
- tableview添加头视图
self.tableView.tableHeaderView = [[UIView alloc]init];
相关代理方法
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{}
- UICollectionView 创建之前需要先写布局,每个collection都需要遵从这个布
局。
UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayoutalloc]init];
layout.sectionInset =UIEdgeInsetsMake(0,0, 0, 0);
layout.headerReferenceSize =CGSizeMake(YJTViewWidth,50*YJTRatioH);//头视图大小
[_collection registerClass:[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;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(头视图的宽, 头视图的高);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(尾视图的宽, 尾视图的高);
}
网友评论