在View里面

//1.创建UICollectionViewFlowLayout

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

//设置

//1.1设置大小

flowLayout.itemSize=CGSizeMake(90, 90);

//1.2设置左右间距(注意如果给定间距,无法满足屏幕的宽度,设置无效)

flowLayout.minimumInteritemSpacing=10;

//1.3设置上下间距

flowLayout.minimumLineSpacing=20;

//1.4设置活动方向

flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;

//1.5设置Header

flowLayout.headerReferenceSize=CGSizeMake(self.bounds.size.width,100);

//1.6设置Footer

flowLayout.footerReferenceSize=CGSizeMake(self.bounds.size.width, 100);

//1.7设置内边距

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

//2.创建集合视图

self.coll=[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];

//背景颜色默认黑色

self.coll.backgroundColor=[UIColor whiteColor];

//3.添加到页面

[self addSubview:self.coll];

***********************自定义CEll

//视图布局

-(void)addAllViews

{

//视图控件都要加载在contentView上

self.c_imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.8)];

self.c_imageView.backgroundColor=[UIColor redColor];

[self.contentView addSubview:self.c_imageView];

self.c_label=[[UILabel alloc] init];

self.c_label.frame=CGRectMake(CGRectGetMinX(self.c_imageView.frame), CGRectGetMaxY(self.c_imageView.frame),self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.2);

self.c_label.backgroundColor=[UIColor cyanColor];

[self.contentView addSubview:self.c_label];

}

//

-(void)layoutSubviews

{

[super layoutSubviews];

self.c_imageView.frame=CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.8);

self.c_label.frame=CGRectMake(CGRectGetMinX(self.c_imageView.frame), CGRectGetMaxY(self.c_imageView.frame),self.contentView.bounds.size.width, self.contentView.bounds.size.height*0.2);

}

**********************************

#import "RootViewController.h"

#import "RootView.h"

#import "Mycell.h"

//标识

#define kcollCell @"collCell"

#define kHeaderView @"headerView"

#define kFooterView @"footerView"

//4.遵循集合视图的代理,数据协议

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

//FlowLayout不需要设置代理

@property(nonatomic,strong)RootView *rv;

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

self.rv=[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];

}

return self;

}

-(void)loadView

{

self.view=self.rv;

}

- (void)viewDidLoad

{

[super viewDidLoad];

//5.设置代理

self.rv.coll.dataSource=self;

self.rv.coll.delegate=self;

// Do any additional setup after loading the view.

//6.注册cell

[self.rv.coll registerClass:[Mycell class] forCellWithReuseIdentifier:kcollCell];

}

//每个分组多少个

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

{

return 102;

}

//返回cell

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

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

{

//7.创建cell(改为自己的cell)

Mycell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:kcollCell forIndexPath:indexPath];

cell.c_imageView.image=[UIImage imageNamed:@"1347722492473[1].jpg"];

cell.c_label.text=@"妞儿";

//注册header

[self.rv.coll registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderView];

//注册footer

[self.rv.coll registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooterView];

float red=arc4random()%256/255.0;

float green=arc4random()%256/255.0;

float blue=arc4random()%256/255.0;

cell.backgroundColor=[UIColor colorWithRed:red green:green blue:blue alpha:0.5];

return cell;

}

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

{

NSLog(@"section: %d,row:%d",indexPath.section,indexPath.row);

}

//设置header和footer

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

{

if (kind==UICollectionElementKindSectionHeader) {

UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderView forIndexPath:indexPath];

UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

label.backgroundColor=[UIColor blueColor];

[header addSubview:label];

label.text=@"哈哈";

header.backgroundColor=[UIColor redColor];

return header;

}

else

{

UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFooterView forIndexPath:indexPath];

footer.backgroundColor=[UIColor redColor];

return footer;

}

}

//

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

{

if (indexPath.row%2) {

return CGSizeMake(90, 90);

}

else

{

return CGSizeMake(60, 60);

}

}

//内边距

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

{

if (section==0) {

return  UIEdgeInsetsMake(10, 10, 10, 10);

}

else

{

return UIEdgeInsetsMake(90, 90, 90, 90);

}

}

//上下间距

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

{

if(section==0)

{

return 10;

}

else

{

return 20;

}

}

//左右间距

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

{

return 10;

}

//

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

{

return CGSizeMake(200,100 );

}

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

{

return  CGSizeMake(200, 100);

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

UICollectionView 集合视图用法,自定义Cell的更多相关文章

  1. UICollectionView集合视图的概念

    如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...

  2. UICollectionView 集合视图 的使用

    直接上代码: // // RootViewController.m // // #import "RootViewController.h" #import "Colle ...

  3. UI学习笔记---第十一天UITableView表视图高级-自定义cell

    自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代 ...

  4. UICollectionView(集合视图)以及自定义集合视图

    一.UICollectionView集合视图           其继承自UIScrollView.         UICollectionView类是iOS6新引进的API,用于展示集合视图,布局 ...

  5. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  6. swift:创建集合视图UICollectionView

    swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...

  7. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  8. List<T>集合的Sort自定义排序用法简单解析

    List<T>集合的Sort自定义排序用法简单解析: 如下:一系列无序数字,如果想要他们倒序排列,则使用如下代码: 那么如何理解这段代码呢? (x,y)表示相邻的两个对象,如果满足条件:x ...

  9. 集合视图UICollectionView 介绍及其示例程序

    UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可 能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的 ...

随机推荐

  1. Javascript经典实例 - 字符串

    1] 'this is a string'这是字符串直接量,new String('this is a string')这是字符串对象,字符串对象可以用字符串对象所带的属性和方法,直接量在“表面上”也 ...

  2. ububru下 redmine安装教程

    1.安装之前确认你已经安装好了mysql 2.连接mysql创建数据库 mysql -u root –p CREATE DATABASE redmine CHARACTER SET utf8; CRE ...

  3. DIRECTORY_SEPARATOR的作用

    IRECTORY_SEPARATOR是php的内部常量,用于显示系统分隔符的命令,不需要任何定义与包含即可直接使用. 在windows下路径分隔符是/(当然/在部分系统上也是可以正常运行的),在lin ...

  4. linux服务器之间拷贝文件和文件夹

    传输文件用法:scp 本机文件目录 远程服务器用户名@服务器IP:/服务器目录 示例:

  5. Delphi泛型评测(30篇)

    http://www.cnblogs.com/jxgxy/category/216671.html

  6. SQL Server 视图

    视图实际上就是一个存储查询,重点是是可以混合和匹配来自基本表(或者其他视图)的数据,从而创建在很多方面像另一个基表那样起作用的对象.可以创建一个简单的查询,仅仅从一个表中选择几列,而忽略其他列:或者也 ...

  7. C# 获取当前应用程序的绝对路径支持asp.net

      Asp.net在类库中获取某文件的绝对路径.这个问题在初学的时候就经常碰到过,经常是查了忘,忘了查.浪费了大量的今天专门写个文章,以后到这里查.有时间顺便记得研究下这个东西. 在主程序目录就不说了 ...

  8. Eclipse总是自动关闭

    -Dosgi.requiredJavaVersion=1.5把INI文件中的这一行删除掉,貌似这样以后模拟器也没再出现timeout的问题了,O(∩_∩)O哈哈~

  9. SQLServer 2000 Driver for JDBC][SQLServer]传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确解决方法

    问题:[SQLServer 2000 Driver for JDBC][SQLServer]传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确.参数 1 (""): ...

  10. 【转】通过 ulimit 改善系统性能

    概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...