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. SendMessage()、WPARAM、LPARAM函数使用例子(转)

      http://chujiaba.blog.163.com/blog/static/18991813720106209350592/ 2010-07-20 21:35:00|  分类: C |  标 ...

  2. 【JAVA版】Storm程序整合Kafka、Mongodb示例及部署

    一.环境 一台Centos6.5主机 Mongo 3.0 Kafka_2.11-0.8.2.1 Storm-0.9.5 Zookeeper-3.4.6 java 1.7 (后因在mac上打包的jar由 ...

  3. Struts2运行流程分析

    一.Struts2运行流程图: 二.运行流程分析: 1. 请求发送给StrutsPrepareAndExecuteFilter 2.StrutsPrepareAndExecuteFilter询问Act ...

  4. 嵌入式LINUX入门到实践(一)

    MINI2440 IIC协议 IIC协议在工程中应用广泛,在我看来,此协议的优势就在于其硬件及其简单,结构清晰. 首先来解读一下S3C2440A这款芯片的IIC协议. 一.一个协议的解读从如上结构图中 ...

  5. CentOS 下安装操作Memcached

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  6. CodeForces 705B Spider Man (水题)

    题意:给定 n 个数,表示不同的环,然后把环拆成全是1,每次只能拆成两个,问你有多少次. 析:也不难,反正都要变成1,所以把所有的数都减1,再求和即可. 代码如下: #pragma comment(l ...

  7. jquery validation ajax 验证

    <link href="${base}/res/basic/css/jquery/validationEngine/validationEngine.jquery.css" ...

  8. rqnoj-390-地震了!-动态规划

    一步步的往前走,判断当前状态与上一个状态的关闭. 注意,题目输入的楼层的速度是从小到大,而实际运用的楼层顺序是从大到小.. #include<stdio.h> #include<al ...

  9. java web,生成验证码图片的技术

    偶然知道原来有些网站的验证码图片都是随机生成的,后来听人讲了一下,就做了这个小例子 生成图片,绘制背景,数字,干扰线用到了java.awt包,主要使用BufferedImage来生成图片,然后使用Gr ...

  10. ssl配置

    Apache SSL配置 作者: JeremyWei | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://weizhifeng.net/apache-ssl.h ...