UICollectionView 的使用是跟表的使用是一样,瀑布流的布局会比表的效果更好,这里说一下 collectionView 设置表头, 区头,区尾

设置表头可以约束 collectionView 居上的距离

其中区头,区尾 是继承 UICollectionReusableView

 //
// HomePageViewController.m
// MainStoryboard
//
// Created by mac on 16/4/21.
// Copyright © 2016年 mac. All rights reserved.
// #import "HomePageViewController.h"
#import "ZMZScrollView.h"
#import "ZkCollectionViewCell.h"
#import "MyHeaderViewView.h" #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define KScreenHeight [UIScreen mainScreen].bounds.size.height #define ARMCOLOR [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1] @interface HomePageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate> @property (nonatomic, strong) UICollectionView* collectionView ; @end //设置标识
static NSString * indentify = @"indentify"; @implementation HomePageViewController - (void)viewDidLoad { [super viewDidLoad]; /**
* 先创建 collectionView 在创建下面的 scrollView
*/ [self waterfall]; [self createScrollView]; }
#pragma mark 轮播广告
- (void)createScrollView
{
ZMZScrollView *ZScroller = [[ZMZScrollView alloc]initWithFrame:CGRectMake(, -, kScreenWidth, )]; ZScroller.imageNameArray = @[@"http://upload.chadaodian.com/mobile/special/s0/s0_05134290739539518.jpg",@"http://upload.chadaodian.com/mobile/special/s0/s0_05134291123904050.jpg",@"http://upload.chadaodian.com/mobile/special/s0/s0_05134291206563185.jpg"]; ZScroller.backgroundColor = [UIColor redColor]; [self.collectionView addSubview:ZScroller]; }
-(void)waterfall{ UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing = 1.0f; [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kScreenWidth, KScreenHeight-64.0f) collectionViewLayout:flowLayout]; self.collectionView.backgroundColor = [UIColor colorWithRed: / 255.0f green: / 255.0f blue: / 255.0f alpha:1.0f]; self.collectionView.contentInset = UIEdgeInsetsMake(200.0f, 0.0f, 0.0f, 0.0f); self.collectionView.delegate=self;
self.collectionView.dataSource = self; [self.view addSubview:self.collectionView]; #pragma mark -- 头部大小设置
[flowLayout setHeaderReferenceSize:CGSizeMake(kScreenWidth, 40.0f)];
/**
* 这里还可以设置区尾的大小
*/ #pragma mark -- 注册单元格
[self.collectionView registerNib:[UINib nibWithNibName:@"ZkCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:indentify]; #pragma mark -- 注册头部视图
[self.collectionView registerClass:[MyHeaderViewView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; /**
* 这里注册区尾
*
*/
} //设置头部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil; if (kind == UICollectionElementKindSectionHeader) {
//定制头部视图的内容
MyHeaderViewView * headerV =(MyHeaderViewView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; headerV.titleLab.text = [NSString stringWithFormat:@"section %ld's header",(long)indexPath.section];; reusableView = headerV;
}
/**
* 在这里设置区尾 UICollectionElementKindSectionFooter
*/ return reusableView;
} #pragma mark - UICollectionViewDelegateWaterfallLayout //返回 indexPath 处的 item 的大小。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{ return CGSizeMake(170.0f, 220.0f); }
//设定全局的行间距,如果想要设定指定区内Cell的最小行距,
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 15.0f;
} //设定collectionView(指定区)的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
} #pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return ;
} - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return ;
} - (UICollectionViewCell* )collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ZkCollectionViewCell* cell =[collectionView dequeueReusableCellWithReuseIdentifier:indentify forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor]; cell.synopsisLabel.text=@"";
return cell;
} //点击单元格
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld区--%ld单元格",(long)indexPath.section,(long)indexPath.row);
} @end

使用 collectionView 实现表头,区头,区尾的更多相关文章

  1. TableView之表头、表尾,区头、区尾!

    一.UITableView的UITableViewStyle 样式分为UITableViewStylePlain和UITableViewStyleGrouped两种: plain样式下区头和区尾是悬浮 ...

  2. tableView区头不显示

    不知道什么原因 如果设置tableView的样式为Group 则必须写代理 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; co ...

  3. tableIView 区头的一点问题

    要记得设置区头的高度 否则会出现第一行没区头问题

  4. UITableView的plain样式下,取消区头停滞效果

    核心代码 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = sectionH ...

  5. C++中栈区 堆区 常量区

    原文地址:http://blog.csdn.net/xcyuzhen/article/details/4543264 C++中栈区 堆区 常量区(由一道面试题目而学习) -- : #include&l ...

  6. iOS之UITableView组头组尾视图/标题悬停

    最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...

  7. iOS程序中的内存分配 栈区堆区全局区

    在计算机系统中,运行的应用程序的数据都是保存在内存中的,不同类型的数据,保存的内存区域不同.一.内存分区 栈区(stack) 由编译器自动分配并释放,存放函数的参数值,局部变量等.栈是系统数据结构,对 ...

  8. Android ListView相关 头和尾 headView footerView

    ListView还可以添加头和尾部,而这头和尾就是View对象, 可以使用listView.addHeadView(view)方法和listView.addFootView(view)方法分别添加头和 ...

  9. 【京东账户】——Mysql/PHP/Ajax爬坑之页头页尾加载

    一.引言 实现京东的账户项目,有一个小功能,页头页尾加载.要用到的是Apach环境,Mysql.PHP以及Ajax. 二.实现 原理: 用php文件分别写一个的页头和一个页尾,放在前后两个div里. ...

随机推荐

  1. svn跳过某个目录

    svn up --set-depth exclude dir2 http://stackoverflow.com/questions/1439176/svn-can-you-remove-direct ...

  2. 百度,你家云管家能靠谱点不?替你脸红!Shame on you!

    此文已提交百度云问题反馈, 坐等答复. 笔者最近下载某24+G分卷压缩文件, 24+G啊, 足足要下将近7个小时.满心欢喜的下载完却尼玛发现解压出错, 有6个文件无法解压?wrong password ...

  3. 配置Tomcat以指定的身份(非root)运行

    本文依赖的环境: CentOS(大部分内容适用于其他Linux发行版) 已安装并配置好JVM环境 已安装并配置好gcc.make等编译工具 1. 下载Tomcat安装包并解压缩 cd /optwget ...

  4. 从一个开发的角度看负载均衡和LVS(转)

    原文:http://blog.hesey.net/2013/02/introduce-to-load-balance-and-lvs-briefly.html 在大规模互联网应用中,负载均衡设备是必不 ...

  5. Linux优化之IO子系统监控与调优

    Linux优化之IO子系统 作为服务器主机来讲,最大的两个IO类型 : 1.磁盘IO 2.网络IO 这是我们调整最多的两个部分所在 磁盘IO是如何实现的 在内存调优中,一直在讲到为了加速性能,linu ...

  6. C语言简单实现sizeof功能代码

    sizeof不是函数,而是运算符,C/C++语言编译器在预编译阶段的时候就已经处理完了sizeof的问题,也就是说sizeof类似于宏定义. 下面给出一个sizeof的一个宏定义实现版本 #defin ...

  7. mysql数据库表间内外链接详解

    1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制)(2)右外连接(右边的表不加限制)(3)全外连接(左右两表都不加限制) 3. 自连接(同一张表内的连接) SQL的标准语法: ...

  8. MyEclipse10.0 安装 jbpm4.4

    马上送上地址:http://sourceforge.net/projects/jbpm/files/jBPM%204/ 偶这里下载的是 jbpm4.4 如图: 1.点击 add site 2.点击 a ...

  9. PostgreSQL的 initdb 源代码分析之二十

    继续分析: setup_privileges(); 展开: 这是设置权限. 其cmd是:"/home/pgsql/project/bin/postgres" --single -F ...

  10. 【M7】千万不要重载&&,||和,操作符

    1.C++对于真假值表达式采用“骤死式”评估方法,比如&&,||. if( p!=NULL && strlen(p)>10)   如果p为NULL,后面的strl ...