ChildModel.h

#import <Foundation/Foundation.h>

@interface ChildModel : NSObject

@property (nonatomic, strong) NSString *imageString;

@property (nonatomic, strong) NSString *nameString;

- (instancetype)initWithDic:(NSDictionary *)dic;

@end

ChildModel.m

#import "ChildModel.h"

@implementation ChildModel

- (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self)
{
_imageString = dic[@"image"];
_nameString = dic[@"name"];
}
return self;
} @end

JFCell.h

#import <UIKit/UIKit.h>
#import "ChildModel.h" @interface JFCell : UICollectionViewCell @property (nonatomic ,strong) UIImageView *cellImageView; @property (nonatomic ,strong) UILabel *cellLabel; - (void)updateJFCellWithObj:(ChildModel *)childObj; @end

JFCell.m

#import "JFCell.h"

@implementation JFCell
@synthesize cellImageView,cellLabel;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , frame.size.width, frame.size.height-)];
self.cellImageView.backgroundColor = [UIColor brownColor];
[self addSubview:self.cellImageView]; self.cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(, frame.size.height-, frame.size.width, )];
self.cellLabel.backgroundColor = [UIColor lightGrayColor];
self.cellLabel.font = [UIFont systemFontOfSize:];
self.cellLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.cellLabel];
}
return self;
} - (void)updateJFCellWithObj:(ChildModel *)childObj
{
self.cellImageView.image = [UIImage imageNamed:childObj.imageString];
self.cellLabel.text = childObj.nameString;
}
@end

ViewController.m

#import "ViewController.h"
#import "JFCell.h" //每行的cell个数
#define counts 3
//边距
#define margin 5 static NSString *cellString = @"cell"; @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *JFCollectionView;
NSMutableArray *childArray;
} @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
childArray = [[NSMutableArray alloc] init];
//添加视图
[self addCollectionView];
//请求数据
[self requestData]; //-------------------①用户请求
} - (void)addCollectionView
{
CGFloat cellWidch = ([[UIScreen mainScreen] bounds].size.width-(counts+)*margin)/counts;
//专门负责布局的类
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//滑动方向(纵向)
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//元素的size
flowLayout.itemSize = CGSizeMake(cellWidch, cellWidch+);
//元素的横向间距
flowLayout.minimumLineSpacing = margin;
//元素的纵向间距
flowLayout.minimumInteritemSpacing = margin;
//组距离 视图边上下左右的距离
flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
JFCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) collectionViewLayout:flowLayout];
JFCollectionView.backgroundColor = [UIColor whiteColor];
JFCollectionView.dataSource = self;
JFCollectionView.delegate = self;
//注册cell,重用
[JFCollectionView registerClass:[JFCell class] forCellWithReuseIdentifier:cellString];
[self.view addSubview:JFCollectionView];
}
#pragma mark ----------------------UICollectionViewDataSource-------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return childArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
JFCell *cell = [JFCollectionView dequeueReusableCellWithReuseIdentifier:cellString forIndexPath:indexPath];
//根据数据更新视图
[cell updateJFCellWithObj:childArray[indexPath.row]]; //------④更新视图,响应用户请求
return cell;
} - (void)requestData
{
//构造虚拟数据(通常是从网络服务器获取到Json数据)
NSArray *dataArray = @[
@{@"image":@"1.jpg",@"name":@"小美"},
@{@"image":@"2.jpg",@"name":@"小娜"},
@{@"image":@"4.jpg",@"name":@"小明"},
@{@"image":@"3.jpg",@"name":@"小娜"},
@{@"image":@"1.jpg",@"name":@"小美"},
@{@"image":@"5.jpg",@"name":@"小娜"},
@{@"image":@"6.jpg",@"name":@"小美"},
@{@"image":@"7.jpg",@"name":@"小娜"},
@{@"image":@"8.jpg",@"name":@"小苏"},
@{@"image":@"2.jpg",@"name":@"小娜"},
@{@"image":@"3.jpg",@"name":@"小娜"},
@{@"image":@"1.jpg",@"name":@"小娜"},
@{@"image":@"3.jpg",@"name":@"小娜"},
@{@"image":@"4.jpg",@"name":@"小娜"},
@{@"image":@"5.jpg",@"name":@"小娜"},
];
for (NSDictionary *dic in dataArray)
{
ChildModel *model = [[ChildModel alloc] initWithDic:dic]; //----------②请求更新数据
[childArray addObject:model];
}
//得到数据后更新视图
[JFCollectionView reloadData]; //------------③通知视图更新
}
@end

UICollectionView的简单使用的更多相关文章

  1. UICollectionView 很简单的写个瀑布流

    你项目中要用到它吗? 可能会在你的项目中用到这玩意,最近也是要用就简单的写了一个 Demo.没多少代码,就不放Git了,下面会详细点的说说代码的,要还有什么问题的小伙伴可以直接Q我,也可以把Demo发 ...

  2. UICollectionView的简单认识和简单实用

    摘要 UICollectionView是比UITableView更加复杂的UI控件,通过它可以实现许多复杂的流布局.但对我们来说,系统提供的接口十分简单易用,并且有十分强的制定性. iOS流布局UIC ...

  3. iOS 流布局 UICollectionView使用(简单使用)

    简介 UICollectionView是iOS6之后引入的一个新的UI控件,它和UITableView有着诸多的相似之处,其中许多代理方法都十分类似.简单来说,UICollectionView是比UI ...

  4. UICollectionView的简单使用和常用代理方法

    UICollectionView相对于UITableView有更加自由的布局,做出的界面可变性更大最近开始接触使用UICollectionView,整理了一下常用的代理方法 首先需要先添加UIColl ...

  5. UICollectionView进阶练习

    上一篇中的干货看完,不觉感觉还是有点虚,今天我们来点实的,做了两个小DEMO,源码已放GitHub,主要是针对UICollectionView做了联系.第一个DEMO是针对UICollectionVi ...

  6. iOS流布局UICollectionView使用FlowLayout进行更灵活布局

    一.引言 前面的博客介绍了UICollectionView的相关方法和其协议中的方法,但对布局的管理类UICollectionViewFlowLayout没有着重探讨,这篇博客介绍关于布局的相关设置和 ...

  7. IOS客户端Coding项目记录导航

    IOS客户端Coding项目记录(一) a:UITextField设置出现清除按键 b:绘画一条下划线  表格一些设置 c:可以定义表头跟底部视图(代码接上面) d:隐藏本页的导航栏 e:UIEdge ...

  8. IOS客户端Coding项目记录(二)

    9:第三方插件整理 JSON转实体:jsonModel https://github.com/icanzilb/JSONModel/ 美化按键:BButton https://github.com/m ...

  9. iOS-UICollectionView快速构造/拖拽重排/轮播实现

    代码地址如下:http://www.demodashi.com/demo/11366.html 目录 UICollectionView的定义 UICollectionView快速构建GridView网 ...

随机推荐

  1. AutoLayout框架Masonry使用心得

    AutoLayout框架Masonry使用心得 字数1769 阅读1481 评论1 喜欢17 我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayout,会后我决定将很久前挖的一个坑给填起 ...

  2. 转载 在.net中使用GAC

    转载出处 https://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的d ...

  3. charindex的用法

    declare @str nvarchar(50);set @str='462,464,2';select @str as '字符串'select len(@str) as '字符长度' select ...

  4. Postfix上的反垃圾邮件的四个方法

    在介绍如何配置Postfix的smtp配置之前有必要首先介绍一下它的背景和特点.Postfix是一个由IBM资助下由WietseVenema 负责开发的自由软件工程的一个产物,其目的是为用户提供除se ...

  5. 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)

    将以下命令保存为 HostsModify.ps1,然后执行即可 #该脚本用来添加hosts解析记录.脚本在执行的时候会判断当前用户是否为管理员,如果不是则弹出提示框口,要求输入相应密码 If (-NO ...

  6. BZOJ 1151 傲娇的人 排序

    傲娇的人 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1151 Descrip ...

  7. 块设备驱动之NAND FLASH驱动程序

    转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/25240909 一.框架总结 watermark/2/text/aHR0cDov ...

  8. JFinal使用笔记1-部署demo项目到本地tomcat

    http://my.oschina.net/u/173975/blog/110261 尝试用JFinal开发个开源的社团管理系统,把开发过程中遇到的问题和解决办法记下来,以供参考. 部署jfinal_ ...

  9. [Bootstrap] 1. container & container-fluid

    Container: 居中 <!DOCTYPE html> <html> <head> <title>Blasting Off With Bootstr ...

  10. sap 三代出口(BADI)的查找方法

    sap 三代出口(BADI)的查找方法 对于根据事务代码查找对应的BADI,网上介绍的方法很多,但总结下来无非就两种方法,在此把它记录下来,方便以后自己查阅了.(1)通过SE24,输入CL_EXITH ...