自定义UICollectionView的头视图或者尾视图(UICollectionReusableView)
来源:互联网 发布:网页图片制作软件 编辑:程序博客网 时间:2024/06/05 04:55
其实看标题就知道是需要继承于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
效果如下图:
0 0
- 自定义UICollectionView的头视图或者尾视图(UICollectionReusableView)
- UICollectionView、UICollectionReusableView头视图重叠问题
- UICollectionView和UICollectionReusableView的使用(集合视图)
- 给UIWebView对象添加自定义的头视图或者尾视图
- 谈谈UICollectionView添加头视图的方法
- 给UICollectionView添加头视图
- UICollectionView section的头视图 复用BUG解决
- CollectionView设置UICollectionReusableView头尾视图
- 代码添加UICollectionView添加头视图
- iOS开发-------自定义简单的表情键盘(UICollectionView 集合视图)
- UICollectionView 集合视图 的使用
- UICollectionView的基本使用完整版(包括UICollectionReusableView)
- ListView的头视图
- UICollectionView 的应用 (集合视图)
- CYC-集合视图 UICollectionView的简单使用
- UICollectionView重用时的视图重叠
- UITableView ,UICollectionView,视图刷新的时候用
- iOS中UICollectionView(集合视图)的使用
- HPUX 无法启动 卡在EVN_MC_INITIATED
- C#编程中Console.Write()和Console.WriteLine()的区别
- C#学习笔记006-字符串
- 编写一个使用指针的c函数,交换数组a和数组b中的对应元素
- C# Console.WriteLine()函数中{}输出格式详解
- 自定义UICollectionView的头视图或者尾视图(UICollectionReusableView)
- 编程输入一行文字,找出其中的大写字母,小写字母个数
- Android Studio开发文档doc显示问题
- 编程输入一行文字,找出其中的大写字母,小写字母个数
- 解读Unity中的CG编写Shader系列1——初识CG
- TableView编辑
- C#使用StreamWriter类写入文件文件
- tomcat的session共享
- JavaHL(JNI) Not Available