本文共 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/