iOS的UICollectionView添加header
2017-07-19 18:59
295 查看
实现效果
一、创建UICollectionView
二、代理方法
一、创建UICollectionView
#import "HomeViewController.h" #import "ConstomCell.h" static NSString *headerViewIdentifier = @"hederview"; @interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> @property (nonatomic,strong) UIImageView *headerImage; @end @implementation HomeViewController - (void)viewDidLoad { [super viewDidLoad]; //1.添加collectionview [self addCollectionView]; } -(void)addCollectionView { UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; //设置每一行的间距 layout.minimumLineSpacing=20; //设置每个单元格的大小 layout.itemSize=CGSizeMake(100, 100); layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0); //设置collectionView头视图的大小 layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250); UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout]; collectionView.frame=self.view.bounds; //注册cell单元格 [collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"]; //注册头视图 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier]; collectionView.backgroundColor=[UIColor whiteColor]; collectionView.delegate=self; collectionView.dataSource=self; [self.view addSubview:collectionView]; }
二、代理方法
#pragma mark 返回多少行 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 13; } #pragma markk 返回的单元格 -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; return cell; } // 返回头视图 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { //如果是头视图 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath]; //添加头视图的内容 [self addContent]; //头视图添加view [header addSubview:self.headerImage]; return header; } //如果底部视图 // if([kind isEqualToString:UICollectionElementKindSectionFooter]){ // } return nil; } /* * 补充头部内容 */ -(void)addContent { UIImageView *headerImage=[[UIImageView alloc]init]; headerImage.contentMode=UIViewContentModeScaleAspectFill; headerImage.clipsToBounds=YES; headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250); headerImage.image=[UIImage imageNamed:@"mei"]; self.headerImage=headerImage; } @end
相关文章推荐
- IOS UICollectionView 添加Header无法正常显示问题解决
- iOS开发之UICollectionView的headerView实现与TableView一样的漂浮效果
- UICollectionView添加 HeaderView FooterView
- 怎样在UICollectionView中添加Header和footer
- [iOS]swift之UITableView添加通过xib创建的headerView坑爹问题
- IOS菜鸟的所感所思(四)——自定义UICollectionViewCell以及网络数据的添加。
- StroyBoard中UICollectionView中添加Header和footer
- 怎样在UICollectionView中添加Header和footer
- iOS中UICollectionView添加头视图的 4000 方法
- [iOS]UITableView添加通过xib创建的headerView坑爹问题
- [转]怎样在UICollectionView中添加Header和footer
- IOS-给UIScrollView(包括继承它的UITableView、UICollectionView)添加下拉刷新-上拉加载更多
- iOS UICollectionView 入门 06 添加标题view
- 如何纯代码给UICollectionView添加HeaderView和FooterView
- [iOS]对TableHeaderView添加约束后适配高度
- iOS 给TableView添加Footer或Header
- 怎样在UICollectionView中添加Header和footer
- iOS 给TableView添加Footer或Header
- iOS UICollectionView、UICollectionViewCell和Header、Footer
- ios开发UI篇—在ImageView中添加按钮以及Tag的参数说明