#import <UIKit/UIKit.h> @interface myHeadView : UICollectionReusableView { UILabel *TitleLable; } -(void)setTextTitle; @end
#import "myHeadView.h" @implementation myHeadView -(instancetype)initWithFrame:(CGRect)frame{ if (self =[super initWithFrame:frame]) { [self createUI]; } return self; } -(void)createUI{ TitleLable= [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)]; [self addSubview:TitleLable]; } //这是头部 -(void)setTextTitle { TitleLable.text=@"哈哈"; } @end
#import "ViewController.h" #import "myHeadView.h" #import "myHeadView.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> { UICollectionView *_myConllectionView; NSMutableArray *_dataArray; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _dataArray =[[NSMutableArray alloc]initWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor grayColor],[UIColor lightGrayColor],[UIColor purpleColor], nil]; UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; [layout setHeaderReferenceSize:CGSizeMake(320, 50)]; //设置headview 的大小 layout.minimumInteritemSpacing=10; //cell之间左右的 layout.minimumLineSpacing=10; //cell上下间隔 layout.itemSize=CGSizeMake(80,140); //cell的大小 layout.sectionInset=UIEdgeInsetsMake(5, 9, 5, 9); layout.headerReferenceSize = CGSizeMake(320, 50); _myConllectionView =[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout]; [_myConllectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; [_myConllectionView registerClass:[myHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView"]; _myConllectionView.delegate=self; _myConllectionView.dataSource=self; [self.view addSubview:_myConllectionView]; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _dataArray.count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.backgroundColor=[_dataArray objectAtIndex:indexPath.row]; return cell; } -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; NSLog(@"kind = %@", kind); if (kind == UICollectionElementKindSectionHeader){ myHeadView *headerV = (myHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView" forIndexPath:indexPath]; [headerV setTextTitle]; reusableview = headerV; } return reusableview; } @end
相关推荐
iOS UICollectionView 纯代码实现布局,以及纯代码实现Section header 头视图,以及 Section footer 尾部视图。纯代码,无xib
用UICollectionView集合视图布局iOS里面的表格。更适用初学UICollectionView控件的小介绍。
XML布局文件和代码混合使用控制视图,学习小案例。
一行代码给tableView添加头部缩放滑动视图
主要介绍了IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView的相关资料,需要的朋友可以参考下
RDHCollectionViewGridLayout 在... pod 'RDHCollectionViewGridLayout' , '~> 1.2'概要此布局提供了用于将集合视图定制为网格布局的简单选项。 支持垂直和水平滚动方向,该布局还照顾到行尾的剩余像素。 例如, UICol
CustomErrorViewLayout一个可以快速给你的布局添加错误视图的库,而且非常轻量级
一个方法实现表头部视图缩放
本文实例为大家分享了iOS实现列表下拉放大效果展示的具体代码,供大家参考,具体内容如下 先看效果图 突然发现没有做出来之前都觉得蛮难的,做出来之后就觉得So Easy 大家都有这样的感触吧 做这个就重写 ...
仿照淘宝天猫中的秒杀倒计时设计
CardsAnimationDemo - swift, 《使用 UICollectionView 实现的一个卡片动画》不是直接操作所有 UIView 和 CALayer 的 transform3D 属性来实现整个效果的,而是使用 UICollectionView 来完成所有的视图管理和实现。
使用UICollectionView封装的无限轮播视图, 使用简单, 提供多种属性自由设置
1、在FlowLayout里面的prepareLayout方法中计算所有cell及“底部上拉加载更多视图”的frame 2、在layoutAttributesForElementsInRect中返回上一部我们计算好的frame的集合 3、在collectionViewContentSize中重新...
手势密码,有带头部视图发,头部视图包括错误提醒和app的图标、版本;
网易头部滚动视图
MVC视图代码逻辑MVC视图代码逻辑MVC视图代码逻辑
样本日历使用 UICollectionView 和自定义布局制作的示例日历视图
tableview头部视图拉伸不变形
类似QQ空间表视图下拉头部视图放大效果实现
Visual C++源代码 148 如何使用数据视图显示主从关系表Visual C++源代码 148 如何使用数据视图显示主从关系表Visual C++源代码 148 如何使用数据视图显示主从关系表Visual C++源代码 148 如何使用数据视图显示主从...