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.效果图如下:

转载于:https://www.cnblogs.com/Yun-Longcom/p/5607988.html

自定义UICollectionView相关推荐

  1. IOS15自定义UICollectionView的使用

    IOS15自定义UICollectionView的使用 自定义UIView ,UIButton ,想必大家都非常熟练, 但是自定义UICollectionView 却有时候错误百出. 重写里面这个方法 ...

  2. 自定义UICollectionView布局(类似集五福)

    废话说在前面 效果展示 CustomCarCollectionViewFlowLayout类的定义 类说明 属性说明 internalItemSpacing itemSize sectionEdgeI ...

  3. iOS 自定义 UICollectionView汇总

    文章目录 引言 I .iOS上传图片视图的封装[支持删除和添加] 1.1 demo源码下载: 1.2 计算cell的高度 II.风险商户材料证明视图 2.1 cellV的高度计算 2.2 自定义col ...

  4. iOS UICollectionView的实现

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

  5. UICollectionView——整体总结

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

  6. swift:自定义UICollectionViewFlowLayout

    swift:自定义UICollectionViewFlowLayout 写作目的 UICollectionView是ios中一个十分强大的控件,利用它能够十分简单的实现一些很好看的效果.UIColle ...

  7. ios 圆形旋转菜单_iOS实现滑动弧形菜单的思路与方法

    前言 最近公司的项目中需要用到弧形菜单,起初自定义UICollectionView的layout,但实现出的效果并不符合项目中要求按钮始终垂直于界面.界面始终保持几个按钮等一系列需求(是我水平不够), ...

  8. iOS-UICollectionView

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

  9. 主要责任、 主要技术

    主要责任.主要技术 责任描述:     协助项目经理对产品进行构架,     软件界面架构及实现,多控制器嵌套处理     利用UI设计组提供的UI图片,使用AutoLayout布局设置对APP界面进 ...

最新文章

  1. java DOS 命令行代码
  2. Windows下安装python的pip
  3. 整数的无符号编码和有符号编码
  4. 3D数学基础:图形与游戏开发---随笔五
  5. 理解单片机系统—汇编语言
  6. Spring 常用注入注解(annotation)和其对应xml标签
  7. Tesseract入门-VS2015下调用Tesseract4.0 +win7 64位系统
  8. 解析.DBC文件, 读懂CAN通信矩阵,实现车内信号仿真
  9. kafka处理大数据包
  10. orcadcapture安装_OrCAD Capture CIS初学者快速上手指导教程
  11. KITTI数据集解析和可视化
  12. (Matlab实现)基于蒙特卡洛模拟的大规模电动车充电模型
  13. 基于python的数字印刷体识别_不告诉你我用了它配合Python简简单单开发OCR识别,带你识别手写体、印刷体、身份证等N种,附代码!...
  14. 机器人领域期刊会议汇总
  15. 网络安全-点击劫持(ClickJacking)的原理、攻击及防御
  16. 433 490 868 915Mhz lora频段贴片天线方案 CA-S01 CrossAir贴片天线
  17. 智能蓝牙音箱方案的四大问题
  18. “使用达芬奇软件实现Autosar架构:配置和注意事项“
  19. php时间正序排序,列表的时间排序,应该正序还是倒序?
  20. 深入理解volatile关键字---缓存一致性原理

热门文章

  1. CSS3实战开发:使用CSS3实现photoshop的过滤效果
  2. 修改文档框架:word-多级列表与标题样式相结合
  3. 分享自制的C#和VB Code互转工具
  4. 总结Movies MVC3教程示例的知识点
  5. 细节决定成败:一个公共类库
  6. LeetCode OJ -- Binary Tree Paths
  7. 关于求N个无序数中第K大的数。
  8. 机器学习知识点(二十一)特征选择之岭回归和LASSO
  9. 离线轻量级大数据平台Spark之MLib机器学习库线性回归实例
  10. JavaScript 技术篇-js正则表达式匹配字符串左右两边是否包含空格