UICollectionView显示HeaderView FooterView 不如UITableView那么容易,经常使用会有两种作法:ui
如图所示,设计
好多都在找UICollectionView是否有这么个属性,好比上图说到Accessories
什么的,其实否则。你们首先要搞明白意见事情,header和footer是追加视图,属于layout
中的因此要代码设置section要在UICollectionViewFlowLayout:code
- (UICollectionViewFlowLayout *) flowLayout{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.。。。。。。//各属性设置 flowLayout.headerReferenceSize = CGSizeMake(320.0f, 50.0f); //设置headerView大小 flowLayout.footerReferenceSize = CGSizeMake(320.0f, 50.0f); // 设置footerView大小 return flowLayout; }
- (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.headerReferenceSize = CGSizeMake(320.0f, 50.0f); //设置headerView大小 [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; // 必定要设置 [self.collectionView setCollectionViewLayout:layout]; //(这部分说明能够参见xib设置sectionview) } - (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE.width, WINSIZE.width*7/15)]; [imageView sd_setImageWithURL:[NSURL URLWithString:[UMOnlineConfig getConfigParams:@"GuizeImageUrl"]] placeholderImage:kDefaultImage192_124]; [headerView addSubview:imageView]; return headerView; }
运行结果:get