IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView
2017-01-06 11:01
495 查看
IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView
其实看标题就知道是需要继承于UICollectionReusableView,实现一个满足自己需求的视图.那么如何操作了,看下面代码:
ViewController.m文件中 #import "ViewController.h" #import "LSHControl.h" #import "SHCollectionReusableView.h" #import "SHCollectionViewCell.h" #define SHCollectionViewCellIdetifiler @"CollectionViewCell" #define SHCollectionReusableViewHeader @"CollectionHeader" #define SHCollectionReusableViewFooter @"CollectionFooter" @interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{ NSArray *recipeImages; NSArray *hearderTitleArray; } @property(nonatomic,strong) UICollectionView *rightCollectionView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *mainDishImages = [NSArray arrayWithObjects:@"egg_benedict.jpg", @"full_breakfast.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", nil]; NSArray *drinkDessertImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"green_tea.jpg", @"starbucks_coffee.jpg", @"white_chocolate_donut.jpg", nil]; recipeImages = [NSArray arrayWithObjects:mainDishImages, drinkDessertImages, nil]; hearderTitleArray=@[@"分区1",@"分区2"]; [self createCollectionView]; } -(void)createCollectionView{ UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init]; flowLayout.minimumInteritemSpacing=0.f;//item左右间隔 flowLayout.minimumLineSpacing=5.f;//item上下间隔 flowLayout.sectionInset=UIEdgeInsetsMake(5,15,5, 15);//item对象上下左右的距离 flowLayout.itemSize=CGSizeMake(80, 80);//每一个 item 对象大小 flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;//设置滚动方向,默认垂直方向. flowLayout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 30);//头视图的大小 flowLayout.footerReferenceSize=CGSizeMake(self.view.frame.size.width, 30);//尾视图大小 self.rightCollectionView= [LSHControl createCollectionViewFromFrame:self.view.frame collectionViewLayout:flowLayout dataSource:self delegate:self backgroudColor:[UIColor whiteColor]]; //自定义重用视图 [self.rightCollectionView registerNib:[UINib nibWithNibName:@"SHCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:SHCollectionViewCellIdetifiler]; [self.rightCollectionView registerClass:[SHCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SHCollectionReusableViewHeader]; //使用原有重用视图 [self.rightCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter ]; [self.view addSubview:self.rightCollectionView]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return [recipeImages count]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return [[recipeImages objectAtIndex:section] count]; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionHeader) { SHCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SHCollectionReusableViewHeader forIndexPath:indexPath]; /** * * 注意:虽然这里没有看到明显的initWithFrame方法,但是在获取重用视图的时候,系统会自动调用initWithFrame方法的.所以在initWithFrame里面进行初始化操作,是没有问题的! */ [headerView getSHCollectionReusableViewHearderTitle:hearderTitleArray[indexPath.section]]; reusableview = headerView; } if (kind == UICollectionElementKindSectionFooter) { UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter forIndexPath:indexPath]; /** * 如果头尾视图没什么很多内容,直接创建对应控件进行添加即可,无需自定义. */ footerview.backgroundColor=[UIColor redColor]; reusableview = footerview; } return reusableview; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ /** * * 这里自定义cell,使用xib进行布局.对于如何使用xib创建视图,不明白的,可以看我之前写的一篇文章. */ SHCollectionViewCell *cell = (SHCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:SHCollectionViewCellIdetifiler forIndexPath:indexPath]; UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100]; recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame-2.png"]]; return cell; } @end
自定义UICollectionViewCell,使用 xib进行布局
#import <UIKit/UIKit.h> @interface SHCollectionViewCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UIImageView *recipeImageView; @end #import "SHCollectionViewCell.h" @implementation SHCollectionViewCell @end
自定义UICollectionReusableView,手写代码进行布局
#import <UIKit/UIKit.h> @interface SHCollectionReusableView : UICollectionReusableView /** * 声明相应的数据模型属性,进行赋值操作,获取头视图或尾视图需要的数据.或者提供一个方法获取需要的数据. */ -(void)getSHCollectionReusableViewHearderTitle:(NSString *)title; @end
#import "SHCollectionReusableView.h" #import "LSHControl.h" @interface SHCollectionReusableView (){ UILabel *titleLabel; } @end @implementation SHCollectionReusableView -(id)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { self.backgroundColor=[UIColor greenColor]; [self createBasicView]; } return self; } /** * 进行基本布局操作,根据需求进行. */ -(void)createBasicView{ titleLabel=[LSHControl createLabelWithFrame:CGRectMake(5, 0,self.frame.size.width-50, self.frame.size.height) Font:[UIFont systemFontOfSize:14.0] Text:@"" color:[UIColor grayColor]]; [self addSubview:titleLabel]; } /** * 设置相应的数据 * * @param title */ -(void)getSHCollectionReusableViewHearderTitle:(NSString *)title{ titleLabel.text=title; } @end
效果如下图:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
- iOS自定义UICollectionViewLayout实现瀑布流布局
- iOS自定义UICollectionViewFlowLayout实现图片浏览效果
- 详解在iOS App中自定义和隐藏状态栏的方法
- IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)
- iOS自定义button抖动效果并实现右上角删除按钮
- 实例解析iOS开发中系统音效以及自定义音效的应用
- iOS开发中使用Quartz2D绘图及自定义UIImageView控件
- iOS自定义推送消息提示框
- IOS轻松几步实现自定义转场动画
- Swift自定义iOS中的TabBarController并为其添加动画
- iOS开发之自定义UITextField的方法
相关文章推荐
- 自定义UICollectionView的头视图或者尾视图(UICollectionReusableView)
- iOS - UICollectionView 的自定义头视图
- IOS之UITableView cell自定义视图中插入Tableview
- [IOS]swift自定义uicollectionviewcell
- IOS--UI--LessonCustumView 自定义视图
- iOS - UICollectionView 自定义布局之风火轮[译] (原版为swift,我这里的实现改为OC)
- iOS开发 - UICollectionViewLayout 自定义布局
- IOS_View或者使用CAShapeLayer将视图变圆
- iOS UICollectionView 入门 05 创建自定义UICollectionViewCell
- iOS开发-UITableViewCell(UICollectionViewItem)需要两个手指点击或者长按才可以选中,解决办法
- IOS中集合视图UICollectionView中DecorationView的简易使用方法
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
- 【iOS开发-24】导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- iOS开发-自定义重用机制给ScrollerView添加子视图
- iOS 6 开发----应用集合视图(UICollectionView)-使用xib文件创建集合视图单元格
- IOS瀑布流 通过自定义UICollectionViewController的Layout布局实现
- UICollectionView 集合视图用法,自定义Cell
- IOSUICollectionView 集合视图
- 源码推荐(9.06):collectionView集合视图展开二级视图,可自定义的 iOS 价格日历
- iOS开发 自定义UICollectionViewLayout实现Masonry Layout