1.创建一个UICollectionView工程,点击鼠标右侧按钮选择New File->Cocoa Class->点击Next,Class选项填写一个合理的名称,如:MyCollectionViewCell,然后点击Next。

2.AppDelegate.m文件中导入头文件“#import “ViewController.h””,然后填写如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];

self.window.rootViewController=nav;

return YES;

}

3.ViewController.m文件代码

#import "ViewController.h"

#import "MyCollectionViewCell.h"

#import "Header.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{

UICollectionView  *mainCollectionView;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

self.navigationController.navigationBar.translucent=NO;

self.navigationController.navigationBar.barTintColor=[UIColor purpleColor];

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

//设置headerView的尺寸大小

layout.headerReferenceSize = CGSizeMake(WIDTH, 0);

//该方法也可以设置itemSize

layout.itemSize =CGSizeMake(90, 150);

mainCollectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];//初始化

//注册UICollectionViewCell

[mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

[mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];

mainCollectionView.dataSource=self;

mainCollectionView.delegate=self;

mainCollectionView.backgroundColor=[UIColor whiteColor];

[self.view addSubview:mainCollectionView];

}

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

return 3;

}

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

return 9;

}

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

static NSString *identifier=@"cell";

MyCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

cell.nameLable.text=[NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];

cell.imageView.image=[UIImage imageNamed:@"photo"];

cell.backgroundColor=[UIColor yellowColor];

return cell;

}

//设置每个item的尺寸

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

//    return CGSizeMake(90, 130);

//}

//设置每个item的UIEdgeInsets

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

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

}

//如果一组中有多行item,设置行间距

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

return 10;

}

//设置两个组之间的列间距

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

return 15;

}

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

//width的设置对该方法无影响

return CGSizeMake(300, 30);

}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”

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

{

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];

headerView.backgroundColor =[UIColor grayColor];

//解决重用机制的bug

for (UIView *view in headerView.subviews) {

[view removeFromSuperview];

}

UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];

if (indexPath.section==0) {

label.text = @"食品类";

}

if (indexPath.section==1) {

label.text = @"水果类";

}

if (indexPath.section==2) {

label.text = @"家用类";

}

label.font = [UIFont systemFontOfSize:20];

[headerView addSubview:label];

return headerView;

}

@end

4.MyCollectionView.h文件代码

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell : UICollectionViewCell

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UILabel     *nameLable;

@end

5.MyCollectionView.m文件代码

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{

self=[super initWithFrame:frame];

if (self) {

_imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 70)];

[self addSubview:_imageView];

_nameLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 80, 70, 30)];

_nameLable.textAlignment=NSTextAlignmentCenter;

_nameLable.textColor=[UIColor blueColor];

_nameLable.font=[UIFont systemFontOfSize:16];

_nameLable.backgroundColor=[UIColor grayColor];

[self addSubview:_nameLable];

}

return self;

}

@end

6.效果图如下:

自定义UICollectionView的更多相关文章

  1. 使用纯代码定义UICollectionView和自定义UICollectionViewCell

    1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...

  2. iOS UICollectionView的实现

    ios的UICollectionView并不能在iOS6之前的版本中使用,为了兼容之前的版本需要自定义UICollectionView.写完之后发现人家已经有开源了,下过来看了看发现我是用UIScro ...

  3. swift:自定义UICollectionViewFlowLayout

    写作目的 UICollectionView是ios中一个十分强大的控件,利用它能够十分简单的实现一些很好看的效果.UICollectionView的效果又依赖于UICollectionViewLayo ...

  4. [iOS UI进阶 - 2.2] 彩票Demo v1.2 UICollectionView基本

    A.需要掌握的 设计.实现设置界面 cell的封装 UICollectionView的使用 自定义UICollectionView 抽取控制器父类 "帮助"功能 code sour ...

  5. 关于自定义UICollectionViewLayout的一点个人理解<一>

    自定义UICollectionView,主要会用到以下几个方法: - (void)prepareLayout; 第一次加载layout.刷新layout.以及- (BOOL)shouldInvalid ...

  6. UICollectionView——整体总结

    前言 这几天有时间看了下UICollectionView的东西,才发觉它真的非常强大,很有必要好好学习学习.以前虽然用过几次,但没有系统的整理总结过.这两天我为UICollectionView做一个比 ...

  7. iOS-UICollectionView

    1--------------------------------------------------------------------------------------------------- ...

  8. 用NSCalendar和UICollectionView自定义日历,并实现签到显示

    前一段时间因为工作需要实现了一个可以签到的日历,来记录一下实现的思路 效果如图:   这里的基本需求是: 1,显示用户某个月的签到情况,已经签到的日子打个圈,没有签到且在某个时间范围内的可以签到,其他 ...

  9. iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

随机推荐

  1. error C3872: “0x3000”: 此字符不允许在标识符中使用

    主要是拷贝的程序问题,有错误的空字符. 0x3000是汉语的空格,也就是全角空格,相当于一个汉字,但你又看不见它. 你知道的,像逗号,有半角(,)和全角(,)之分的,其实空格也有. 0x3000是全角 ...

  2. Java Script 学习笔记

    JS编程习惯类: 1. 命名 著名的变量命名规则 只是因为变量名的语法正确,并不意味着就该使用它们.变量还应遵守以下某条著名的命名规则: Camel 标记法 首字母是小写的,接下来的字母都以大写字符开 ...

  3. 移动端默认返回按键,使用h5+修改默认事件

    hbuilder的h5+提供开发webapp的诸多便利,很多手机自带back虚拟按键,如果不修改其默认事件,点一下app就退出了,所以我这里提供一种修改这个按键默认事件事件的代码. 首先你要用hbui ...

  4. fpSpread 设置Border 样式

    // Create a new bevel border. //FarPoint.Win.BevelBorder bevelbrdr = new FarPoint.Win.BevelBorder(Fa ...

  5. 支付宝支付-tp5

    官方文档中第二步请求,获取签名后的订单信息,并没有说清楚需要的数据和数据的格式,这里详细说下. 1.先引入签名和验签的类 2.实例化该类,将下图方框里的数据换成你的 3.拼凑支付宝服务器端需要的数据和 ...

  6. neutron openvswitch agent实现安全组的方法

    关于openstack安全组,采用一问一答的形式记录如下 1. 是加载在计算节点的还是网络节点的? 是加载在计算节点的 2. 是使用iptable规则实现的吗? M版的neutron实现了openvs ...

  7. 【留念贴】Android开发——计算器

    [过程] 在电商学霸&&代码女神XuFei的影响下,接触到了关于Android客户端的一些开发,第一次在Android平台搞出了一个App,真的是激动不已,所以必须开个留念贴记录一下. ...

  8. Spring 创建 IOC 容器 ClassPathXmlApplicationContext 类

    一.ClassPathXmlApplicationContext 类的作用 在 Java 环境中使用 Spring 时,需要用 new ClassPathXmlApplicationContext(c ...

  9. 记第一届 CCCC-团体程序设计天梯赛决赛 参赛

    其他都没什么,上午报道,下午比赛两个半小时,最后139分 但四我超遗憾的是,最后在做L3-1二叉搜索树,因为看到有辣么多人做出来,可是我没做出来啊 比赛结束后看了看其他两道当场吐血,L3-3直捣黄龙不 ...

  10. about hibernate lazy load and solution

    about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...