StroyBoard中UICollectionView中添加Header和footer

  到Storyboard中,选择collection view controller中的"Collection View"。在Attributes inspector中,选择"Section Header"和"Section Footer",一旦选中你就会在屏幕中看到下面的的显示:

 

  最重要的是,我们必须为header和footer view指定一个标识符。这个标示符将会被用于代码识别图片名称。在Atteributes inspector中设置header view的identifier为“HeaderView”,同样的把footer view的identifier设置为“FooterView”。

接下来为Header View 和Footer View添加新类

 在默认情况下,header和footer view和UICollectionResuable类相关联。为了在header view中显示我们需要的内容,我们必须创建一个新的继承自UICollectionResuableView的类,我们可以命名为MyHeaderViewh和MyFooterView。

 

设置好相关Outlet

 

#import "MyHeaderView.h"

#import "MyFooterView.h"

// 定义 Header View Size

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

{

    return CGSizeMake(0, 40);

}

 

// 定义 Footer View Size

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

{

  return CGSizeMake(0, 40);

}

// collection view添加页眉或页脚

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

{

    

    UICollectionReusableView *reusableview = nil;

    

    if (kind == UICollectionElementKindSectionHeader){

        

        MyHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

        

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

        

        headerView.headerTitle.text = title;

        headerView.headerImageView.backgroundColor = [UIColor grayColor];

        

        reusableview = headerView;

        

    }

    else if (kind == UICollectionElementKindSectionFooter){

        

        MyFooterView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier forIndexPath:indexPath];

        

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

        

        footerview.footerTitle.text = title;

        footerview.footerImageView.backgroundColor = [UIColor greenColor];

        

        reusableview = footerview;

        

    }

    

    return reusableview;

    

}

 

// 一下是完整代码

#import "PhotoCollectionViewController.h"

#import "MyHeadView.h"

#import "MyFooterView.h"

 

@interface PhotoCollectionViewController ()

 

@property (nonatomic, strong) NSMutableArray *dataArray;

 

@end

 

@implementation PhotoCollectionViewController

 

static NSString * const reuseIdentifier = @"Cell";

static NSString * const headerIdentifier = @"HeaderView";

static NSString * const footerIdentifier = @"FooterView";

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // Uncomment the following line to preserve selection between presentations

    // self.clearsSelectionOnViewWillAppear = NO;

    

    self.collectionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];

    

    // Register cell classes

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    

    NSMutableArray *array1  = [[NSMutableArray alloc]initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png", nil];

    NSMutableArray *array2  = [[NSMutableArray alloc]initWithObjects:@"image4.png",@"image3.png",@"image2.png", nil];

    NSMutableArray *array3  = [[NSMutableArray alloc]initWithObjects:@"image1.png",@"image2.png", nil];

    NSMutableArray *array4  = [[NSMutableArray alloc]initWithObjects:@"image1.png", nil];

    

    _dataArray = [NSMutableArray arrayWithObjects:array1,array2,array3,array4, nil];

    

    

    // Do any additional setup after loading the view.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

#pragma mark <UICollectionViewDataSource>

 

// 返回collection view里区section的个数

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

    return [_dataArray count];

}

 

// 返回指定区section包含的Items

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

    return [[_dataArray objectAtIndex:section] count];

}

 

#pragma mark <UICollectionViewDelegate>

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

    //重用cell

    PhotoCollectionViewCell *myCell = [collectionView

                                       dequeueReusableCellWithReuseIdentifier:@"photoCell"

                                       forIndexPath:indexPath];

    

    myCell.backgroundColor = [UIColor yellowColor];

    

    

    UIImage *image = [UIImage imageNamed:[[_dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] ];

    myCell.phontImageView.image = image;

    return myCell;

}

 

// 定义每个UICollectionViewCell 的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake((self.view.frame.size.width-40)/3.0, (self.view.frame.size.width-40)/3.0);

}

 

// 定义每个Section margin

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(20, 10, 5, 10);

}

 

// 每个section中不同的行之间的行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

    return 10.0;

}

 

// 定义每个Section margin

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

    return 10.0;

}

 

// indexPath处的item被选择时触发

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

{

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

}

 

// 定义 Header View Size

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

{

    return CGSizeMake(0, 40);

}

 

// 定义 Footer View Size

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

{

  return CGSizeMake(0, 40);

}

 

// collection view添加页眉或页脚

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

{

    

    UICollectionReusableView *reusableview = nil;

    

    if (kind == UICollectionElementKindSectionHeader){

        

        MyHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

        

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

        

        headerView.headerTitle.text = title;

        headerView.headerImageView.backgroundColor = [UIColor grayColor];

        

        reusableview = headerView;

        

    }

    else if (kind == UICollectionElementKindSectionFooter){

        

        MyFooterView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier forIndexPath:indexPath];

        

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

        

        footerview.footerTitle.text = title;

        footerview.footerImageView.backgroundColor = [UIColor greenColor];

        

        reusableview = footerview;

        

    }

    

    return reusableview;

    

}运行效果

转载于:https://www.cnblogs.com/joesen/p/4452850.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/273295.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

树形结构数据汇总查询解决方案+优化求助

最近遇到一个地区数据汇总的问题&#xff0c;地区下的地址呈树形结构&#xff0c;&#xff08;简化结构&#xff09;如A市下有B、C区&#xff0c;B区下有D、E街道。先要查询所有地区的人数&#xff08;包括子区域&#xff09;&#xff0c;如A的人数直属A的人数B的人数C的人数D的…

find 是区分大小写的。对于不区分大小写的写法(转载)

转自&#xff1a;http://justwinit.cn/post/3633/ 默认情况下&#xff0c;find 是区分大小写的。对于不区分大小写的 find&#xff0c;将 -iname 测试替换为 -name 测试。find downloads -iname "*.gif"downloads/.xvpics/Calendar05_enlarged.gifdownloads/lcmgcfe…

ORACLE会话以及SQL执行信息查询

select t.BLOCKING_SESSION,t.SQL_ID,t.SID,t.SERIAL#,t.MACHINE,t.PROGRAM,t.ACTION,t.LOGON_TIME "登录时间",trunc((sysdate - t.LOGON_TIME) * 24 * 60 * 60) || s "登录时长",trunc(nvl(s.ELAPSED_TIME / decode(s.EXECUTIONS, 0, 1, s.EXECUTIONS) /…

Dom4j 学习笔记

dom4j 是一种解析 XML 文档的开放源代码 XML 框架。dom4j下载地址 本文主要记载了一些简单的使用方法。 一、xml文件的解析 dom4j既可以解析普通的xml文件&#xff0c;也可以解析一个InputStream&#xff0c;先看看xml文件长什么样子&#xff1a; <books><book>&l…

交叉连接(CROSS JOIN)的实际应用

一次偶然的机会&#xff0c;使用到了万年不用的交叉连接&#xff08;CROSS JOIN&#xff09; 业务场景如下&#xff1a; 1、存在多个运营商&#xff0c;每个运营商下面都有各种类型的设备&#xff0c;不同运营商的设备不完全相同&#xff1b; 2、任何设备有且仅有两种用途‘…

Atitit.操作注册表 树形数据库 注册表的历史 java版本类库总结

Atitit.操作注册表 树形数据库 注册表的历史 java版本类库总结 1. 注册表是树形数据库 1 2. 注册表的由来 1 3. Java 操作注册表 2 3.1. 使用Preferences API &#xff08;限定访问路径了&#xff09; 2 3.2. 使用JNI 3 3.3. Jregistrykey 推荐 4 3.4. Jregistry 4 4. org.ope…

C# xml文件读取与修改

c#读写xml文件已知有一个XML文件&#xff08;bookstore.xml&#xff09;如下&#xff1a; Code<?xml version"1.0" encoding"gb2312"?><bookstore> <book genre"fantasy" ISBN"2-3631-4"> <title>Obero…

外连接从表过滤

1、使用left join时从表的过滤 WITH a AS( SELECT A aid FROM dual UNION ALL SELECT B FROM dual UNION ALL SELECT C FROM dual UNION ALL SELECT D FROM dual UNION ALL SELECT E FROM dual ), b AS( SELECT A aid,10 num,1 type FROM dual UNION ALL SELECT B,20,2 FROM d…

php pcntl 多进程学习

1、捕获子进程退出&#xff08;监听SIGCHLD信号&#xff0c;然后调用 pcntl_wait 函数&#xff09; declare(ticks1);pcntl_signal(SIGCHLD, "sig_handler"); function sig_handler($signo) {switch ($signo) {case SIGCHLD:$status 0;$child_id pcntl_wait($statu…

Oracle取最大/最小值函数

SELECT greatest(DATE2020-01-01,DATE2020-01-03,DATE2020-01-05,DATE2020-01-07,DATE2020-01-09) 最大值, least(1,3,5,7,9) 最小值 FROM dual; SELECT 1 FROM dual WHERE greatest(1,3,5,7,9) > 8;

ORACLE将查询字段指定为某种类型

SELECT CAST(张三 AS VARCHAR2(20)) name FROM dual; 一般来说在查询时很少有用到这种语句&#xff0c;但是使用CREATE TABLE ... AS SELECT ...语句的时候这个就很好用了 --建表 CREATE TABLE test01 AS SELECT 张三 name FROM dual; --正常插入数据 INSERT INTO test01 SEL…

Less Css 教程

http://www.w3cplus.com/css/less&#xff0c;这个东西太吊了&#xff01;转载于:https://www.cnblogs.com/wln3344/p/4479014.html

分组查询最晚一条数据(ORACLE)

现有客户表&#xff0c;交费表&#xff0c;需查询每个存在交费记录客户的最后一笔交费信息 这里提供两种方式 注&#xff1a;客户不会在同一时间有两条交费&#xff0c;SQL可直接执行 --查询客户名称&#xff0c;最后一笔交费时间&#xff0c;以及最后一笔交费金额 WITH --客…

ORACLE循环中使用序列

在批量生成数据时&#xff0c;经常会用到序列的Nextval&#xff0c;今天碰到了这样的情况&#xff0c;日常记录&#xff0c;下附解决方案。先看这段脚本 DECLARE i INTEGER; BEGINFOR cur IN 1..5 LOOPi : DomainObjectId.Nextval;dbms_output.put_line(i);END LOOP; END; 编…

常用的机器学习数据挖掘知识点【转】

转自&#xff1a; 【基础】常用的机器学习&数据挖掘知识点 Basis(基础)&#xff1a; MSE(Mean Square Error 均方误差)&#xff0c;LMS(LeastMean Square 最小均方)&#xff0c;LSM(Least Square Methods 最小二乘法)&#xff0c;MLE(MaximumLikelihood Estimation最大似然…

tomcat运行问题解决方法

早上过来遇到一个非常奇怪的问题&#xff0c;运行一个新的项目&#xff0c;运行环境都没问题&#xff0c;可是在调试的时候&#xff0c;总是出错。 错误代码&#xff1a; log4j:WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system p…

团队开发——冲刺1.d

冲刺阶段一&#xff08;第四天&#xff09; 1、昨天做了什么&#xff1f; 完成部分界面设置&#xff0c;补充三层难度界面、游戏结束界面。 2、今天准备做什么&#xff1f; 优化界面细节。查看C#资料&#xff0c;再解决自己电脑的问题。 3、遇到什么困难&#xff1f; 已经固定好…

10. javacript高级程序设计-DOM

1. DOM DOM(文档对象模型)是针对HTML和XML文档的一个API&#xff08;应用程序接口&#xff09; 1.1 节点层次 DOM可以将任何HTML和XML文档描绘成一个由多层节点构成的结构。节点分为几种不同的类型&#xff0c;每种类型分别表示文档中不同的信息及标记。 1.1.1 Node类型 DOM1中…

hdu 5045 Contest(状态压缩DP)

题解&#xff1a;我们使用一个二位数组dp[i][j]记录进行到第i个任务时&#xff0c;人组合为j时的最大和&#xff08;这里的j我们用二进制的每位相应一个人&#xff09;。 详细见代码&#xff1a; #include <iostream> #include <cstdio> #include <cstring> …

mysql001创建数据库

-- 注释&#xff0c;ctrl/ -- 查询所有数据库&#xff1b; show DATABASES; -- 创建数据库; CREATE DATABASE studb; -- 切换数据库; USE studb; -- 删除数据库 DROP DATABASE studb;