博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CollectionView设置UICollectionReusableView头尾视图
阅读量:4086 次
发布时间:2019-05-25

本文共 3296 字,大约阅读时间需要 10 分钟。

//  ViewController.m#import "ViewController.h"@interface ViewController ()// 使用sb视图中的FlowLayout@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;@endstatic NSString *ID = @"cell";static NSString *HeaderID = @"header";static NSString *FooterID = @"footer";@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 注册cell    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];    self.collectionView.backgroundColor = [UIColor whiteColor];    // 注册头部    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID];    // 如果有class来注册这个头部或尾部视图时一定要用代码的方式去设置下这个头部或尾部的尺寸    // 加载的时候会根据字符串来判断是头还是尾    self.flowLayout.headerReferenceSize = CGSizeMake(50, 50);    // 注册尾部    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterID];    self.flowLayout.footerReferenceSize = CGSizeMake(50, 50);    // 当滚动方向发生颠倒时头或尾的尺寸设置也会发生颠倒    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;    /**     UICollectionViewFlowLayout中默认情况只有 itemSize和最小行列间距是有值的其它全是0     */    // 判断系统版本9.0以后才有这个功能    if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {        // 当前组如果还在可视范围时让头部视图停留        self.flowLayout.sectionHeadersPinToVisibleBounds = YES;        // 当前组如果还在可视范围时让尾部视图停留        self.flowLayout.sectionFootersPinToVisibleBounds = YES;    }}// 返回格子有多少组,这里自定义有5组- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {    return 5;}// 返回每组有多少个格子,自定义每组有15个格子- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    return 15;}// 返回每一个格子长什么内容:5组,每组15个- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    // 1.创建cell,需要提前注册    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];    // 2.设置数据,设置了一个随机背景色    cell.backgroundColor =  [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];    // 3.返回cell    return cell;}// 返回每一组的头部或尾部视图// 会自动的在每一组的头部和尾部加上这个视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {    // 如果当前想要的是头部视图    // UICollectionElementKindSectionHeader是一个const修饰的字符串常量,所以可以直接使用==比较    if (kind == UICollectionElementKindSectionHeader) {        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID forIndexPath:indexPath];        headerView.backgroundColor = [UIColor greenColor];        return headerView;    } else { // 返回每一组的尾部视图        UICollectionReusableView *footerView =  [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterID forIndexPath:indexPath];        footerView.backgroundColor = [UIColor purpleColor];        return footerView;    }}@end

转载地址:http://hykii.baihongyu.com/

你可能感兴趣的文章
Java8 HashMap集合解析
查看>>
自定义 select 下拉框 多选插件
查看>>
Linux常用统计命令之wc
查看>>
fastcgi_param 详解
查看>>
搞定Java面试中的数据结构问题
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
Winform多线程
查看>>
Spring AOP + Redis + 注解实现redis 分布式锁
查看>>
poj 1976 A Mini Locomotive (dp 二维01背包)
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
生产者消费者模型,循环队列实现
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
字符串的截取
查看>>
剑指_复杂链表的复制
查看>>
FTP 常见问题
查看>>
do_generic_file_read()函数
查看>>
Python学习笔记之数据类型
查看>>