"UICollectionView实现带头视图和组的头视图同时存在"实现
实现效果如下:

以前做这效果的界面,总是实现的是section的头视图,因为我一直觉得collectionView是不像UITableView那样有tableHeaderView的,所以每次实现只能是判断indexpath.section为0的时候,组合一下视图做第一个section的头视图.
今天看别人写的Swift项目,看到人家代码的实现这效果的简便,我实在是不敢相信这么容易,于是自己赶紧用OC写了个简单的demo,发现还真是能实现呢......好开心....
实现的源码如下,注释的很清楚啦:
//
// ViewController.m
// HeaderCollectionView
//
// Created by 思 彭 on 16/10/24.
// Copyright © 2016年 思 彭. All rights reserved.
// #import "ViewController.h"
#import "SectionHeaderView.h" @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation ViewController #pragma mark - life Cycle - (void)viewDidLoad {
[super viewDidLoad];
self.title = @"HeaderCollectionView";
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor = [UIColor whiteColor];
[self setCollectionView];
[self setHeaderView];
} #pragma mark - setUI - (void)setCollectionView { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake((self.view.frame.size.width - ) / , (self.view.frame.size.width - ) / );
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
// 最重要的一句代码!!!
self.collectionView.contentInset = UIEdgeInsetsMake(, , , );
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.collectionView]; // 注册cell
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionCellIdentify"];
[self.collectionView registerNib:[UINib nibWithNibName:@"SectionHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionHeaderView"];
} - (void)setHeaderView { // 注意这里设置headerView的头视图的y坐标一定是从"负值"开始,因为headerView是添加在collectionView上的.
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(, -, self.view.frame.size.width, )];
headerView.backgroundColor = [UIColor greenColor];
[self.collectionView addSubview:headerView];
} #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return ;
} - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return ;
} - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCellIdentify" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
} #pragma mark - UICollectionViewDelegateFlowLayout - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(, , , );
} #pragma mark - UICollectionViewDelegate - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { // 复用SectionHeaderView,SectionHeaderView是xib创建的
SectionHeaderView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionHeaderView" forIndexPath:indexPath];
return headerView; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(self.view.frame.size.width, );
} @end
每天进步一点点......你也加油哟!!
"UICollectionView实现带头视图和组的头视图同时存在"实现的更多相关文章
- 设置tableView的组的头视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1 ...
- UICollectionView(集合视图)以及自定义集合视图
一.UICollectionView集合视图 其继承自UIScrollView. UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...
- Swift自定义头视图和尾视图
var data: [[String]]! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup a ...
- UICollectionview的头视图和尾视图
UITableView有头视图和尾视图,那么UICollectionView有没有头视图和尾视图呢? 答案是有的. 1.新建一个类,必须继承自 UICollectionReusableView. 2. ...
- iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)
两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点: 表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...
- Qt图形视图体系结构示例解析(视图、拖拽、动画)
本博的示例来自与QT Example:C:\Qt\Qt5.9.3\Examples\Qt-5.9.3\widgets\graphicsview\dragdroprobot 将通过分析示例完成主要功能: ...
- iOS - 模态Model视图跳转和Push视图跳转的混合需求实现原理
在研发中总会遇到一些莫名的需求,本着存在即合理的态度跟大家分享一下"模态Model视图跳转和Push视图跳转的需求实现",本文仅仅传授研发技术不传授产品以及UE的思想,请大家合理对 ...
- Oracle数据库入门——如何根据物化视图日志快速刷新物化视图
Oracle物化视图的快速刷新机制是通过物化视图日志完成的.Oracle如何通过一个物化视图日志就可以支持多个物化视图的快速刷新呢,本文简单的描述一下刷新的原理. 首先,看一下物化视图的结构:SQL& ...
- 从模态视图push到另一个视图
//需要给模态视图创建一个Nav,然后再调用presentViewController if (_loginVC == nil) { _loginVC = [[LoginViewController ...
随机推荐
- want
2-SAT POJ3683 操作树 仙人掌 HDU3594 平衡树 916D 可持久化Trie https://www.cnblogs.com/jszkc/p/7309468.html Lindstr ...
- Linux之checkconfig 服务自启动
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法: chkconfig [--a ...
- JS 给数字加三位一逗号间隔的方法
1.方法 function format_number(n) { var b = parseInt(n).toString(); var len = b.length; ) { return b; } ...
- Linux配置python环境2,安装python
安装锁需要的依赖包 sudo apt-get -y install gcc-5 g++-5 libc6-dev make build-essential libssl-dev zlib1g-dev l ...
- 利用fastjson的一些使用小记
//Json converter to objectTouBaoTXTpaicResponse touBaoTXTpaicResponse = JSON.parseObject(json.toJSON ...
- Hadoop-No.6之文件在HDFS中的位置
在设计一种HDFS模式时,首先应该决定文件的位置.标准化的位置会使得团队之间更容易查找和共享数据. 推荐HDFS目录结构实例.目录结构简化了不同组和用户的权限分配 /user/{username} 只 ...
- Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题。(转)
环境 springmvc jdk1.8 maven redis.properties配置文件 #redis setting redis.host=localhost redis.port=6379 r ...
- c++顺序表(数组)查找最大最小值
#include <iostream> #define MAXSIZE 100 using namespace std; void find(int a[],int m) {int min ...
- codeforces1C
Ancient Berland Circus CodeForces - 1C 如今,Berland 的所有马戏表演都有一个圆形的竞技场,其直径为 13 米,但过去的情况有所不同. 在古代 Berlan ...
- 通过nginx转发,用外网连接阿里云的redis,报Unexpected end of stream的解决办法
一.在与redis同一个内网的服务器上A的nginx做了下面的设置 stream { upstream redis { server redis.rds.aliyuncs.com:6379 max_ ...