2019独角兽企业重金招聘Python工程师标准>>>

@interface XCZCollectionHeader : UICollectionReusableView

#import "XCZCollectionsViewController.h"

#import "XCZCollectionViewCell.h"

#import "XCZCollectionHeader.h"

@interface XCZCollectionsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation XCZCollectionsViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = @"分类";

[self createCollectionView];

}

- (void)createCollectionView {

// 第一步:新建布局对象

UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];

CGFloat padding = 10;

CGFloat itemW = (self.view.frame.size.width - 5 * padding)/4;

CGFloat itemH = itemW;

layout.itemSize = CGSizeMake(itemW, itemH);

// 头尾高度

layout.headerReferenceSize = CGSizeMake(0, 10);

// 最小间隔

// 对于垂直滚动,它是行间距

// 对于水平滚动,它是列间距

layout.minimumLineSpacing = 10;

// 内边距

layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

// 集合视图,用于块状分布

// iOS6.0之后出现的视图

// 第二步,新建集合视图,关联布局对象

[self setAutomaticallyAdjustsScrollViewInsets:NO];

UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, [[UIScreen mainScreen] bounds].size.height - 64) collectionViewLayout:layout];

// 第三步,注册cell, 注册追加视图(section的头和尾)

// 注册cell

[collectionView registerClass:[XCZCollectionViewCell class] forCellWithReuseIdentifier:@"MyCellID"]; // 复用标识,可随意写

// 注册supplementary view

// 第一个参数:注册的类

// 第二个参数:追加视图的类型 UICollectionElementKindSectionHeader  表示头,UICollectionElementKindSectionFooter表示尾 相当于tableView 中分区的headerView和footerView

// 第二个参数:复用标识

[collectionView registerClass:[XCZCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MySectionHeaderViewID"]; // 注册头

// 第四步,设置代理,并实现代理方法

collectionView.delegate = self;

collectionView.dataSource = self;

collectionView.backgroundColor = [UIColor whiteColor];

[self.view addSubview:collectionView];

}

#pragma mark - 实现代理方法

// 多少个分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 4;

}

// 每个分区有多少cell(item)

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

if (section == 0) {

return 7;

}else if (section == 1){

return 11;

}else if (section == 2){

return 12;

}else if (section == 3){

return 7;

}

return section;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

// 每个cell是什么

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

XCZCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCellID" forIndexPath:indexPath];

cell.imageView.image = [UIImage imageNamed:@"1"];

cell.textLabel.text = @"嘎嘎";

return cell;

}

// 头尾视图

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

// 根据是头视图还是尾视图作判断

XCZCollectionHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MySectionHeaderViewID" forIndexPath:indexPath];

headerView.backgroundColor = [UIColor grayColor];

NSArray * arr = @[@"选集",@"主题",@"写景",@"节日"];

//    NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group #%li",indexPath.section +1];

headerView.textLabel.text = arr[indexPath.section];

return headerView;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"--%ld",(long)indexPath.row);

}

// 通过代理方法设置headerView的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

return CGSizeMake(0, 26);

}

转载于:https://my.oschina.net/u/2603560/blog/667094

UICollectionView简单使用相关推荐

  1. UICollectionView 简单使用

    显示数据列表 大家通常使用的是UITableView  不用说TableView 是大家的首选.在iOS6之前这也是必选.但是伴随着APP的成长一起都在变化目前更多的呈现一种块状的显示效果.之前的行式 ...

  2. iOS流布局之UICollectionView简单应用

    iOS流布局UICollectionView系列一--初识与简单使用UICollectionView 一.简介 UICollectionView是iOS6之后引入的一个新的UI控件,它和UITable ...

  3. UICollectionView,CollectionView,瀑布流

    UICollectionView 布局自定义 IOS collectionViewCell防止复用的两种方法 Swift - 实现UICollectionView分组头悬停效果(方法1:使用自定义布局 ...

  4. iOS开发-------自定义简单的表情键盘(UICollectionView 集合视图)

    最近制作自制表情键盘的时候,突然了解到还有一个叫做UICollectionView (集合视图)的类,就研究了一下,确实在做表情键盘上要比用 UIScrollView(滚动视图) 要简单的多,用法与 ...

  5. UICollectionView实现的图片的多选效果(本人已封装好,简单操作)

    github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准) 先说操作,有时间再说我的设计原理. 两种模式: 模 ...

  6. iOS UICollectionView的简单使用和常用代理方法

    UICollectionView相对于UITableView有更加自由的布局,做出的界面可变性更大最近开始接触使用UICollectionView,整理了一下常用的代理方法. 首先需要先添加UICol ...

  7. UICollectionView

    UICollectionView 多列的UITableView,最简单的形式,类似于iBooks中书架的布局,书架中放着你下载的和购买的电子书. 最简单的UICollectionView是一个Grid ...

  8. 【iOS官方文档翻译】UICollectionView与UICollectionViewFlowLayout

    (一)先来简单回顾一下UICollectionView *UICollectionView的简单使用可以看我以前写的这篇博文:UICollectionView的基本使用 UICollectionVie ...

  9. iOS UICollectionView实现瀑布流(3)

    前面两篇Blog简单的介绍了UICollection的基本使用并实现了类似Android的Gallery效果,这篇文章使用UICollection来实现瀑布流效果,代码主要是在极客学院Carol老师的 ...

最新文章

  1. Struts2+Spring详解
  2. iPhone开发中的一些小技巧
  3. JavaScript递归算法统计(将整数n分成任意两份不能相同的k份)分法的种数
  4. Ecshop:后台添加新功能栏目以及管理权限设置
  5. cv mat保存图片_(七)神秘的Mat
  6. 关于Web面试的基础知识点--Html+Css(四)
  7. ActionT 委托
  8. 2021年内衣品牌营销传播方案-婧麒+美柚.pdf(附下载链接)
  9. 4S店卖不出去的库存车都是怎么处理的?
  10. 营销和生意人的4项基本修炼
  11. Leetcode 53.最大子序列和
  12. 事件类型-UI事件、焦点事件
  13. 流程图软件,visio,安装下载教程
  14. 【第六届蓝桥杯】奇妙的数字
  15. 公用计算机打不开扫雷,win7系统“扫雷”游戏无法打开问题的处理方法
  16. 犀牛系统连接服务器失败,犀牛5.0安装运行失败,Rhino安装失败解决方法
  17. Termux新/旧版下载地址及其附属包下载地址归总(附低版本aarch64安装包地址)
  18. Java核心基础30天
  19. QUIC特性之连接迁移和队头阻塞消除
  20. (阿里云笔记)购置域名+云解析DNS域名

热门文章

  1. 京东区块链(智臻链):1. 应用场景
  2. 《位置大数据隐私管理》—— 1.4 隐私泄露威胁
  3. C# 实时监控线程类
  4. 计算机无法连接到手机热点,电脑搜不到手机的热点是为什么_电脑无法发现手机热点的处理方法...
  5. 【图文详解】一文全面彻底搞懂HBase、LevelDB、RocksDB等NoSQL背后的存储原理:LSM-tree日志结构合并树...
  6. 【48期】盘点Netty面试常问考点:什么是 Netty 的零拷贝?
  7. java异常处借接错书_Java入门第三季-异常-图书馆借书系统
  8. 单片机毕业设计 自动浇花灌溉系统设计
  9. c语言函数大全表格形式,C语言函数大全[表格形式].doc
  10. 一步一步学Silverlight 2系列(21):如何在Silverlight中调用JavaScriptjavascript