##DAY15——UICollectionView
DAY15——UICollectionView
创建UICollectionView
//创建一个布局对象,采用系统布局类UICollectionViewFlowLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置滑动方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
CGFloat totalWidth = self.view.frame.size.width;
//设置最小的行间距
layout.minimumLineSpacing = ;
//设置item与item之间的间距
layout.minimumInteritemSpacing = ;
//设置集合视图的分区间隔(上、左、下、右)
layout.sectionInset = UIEdgeInsetsMake(, , , );
//设置每一个item的尺寸的大小
layout.itemSize = CGSizeMake((totalWidth - ) / , );
//集合视图的创建必须指定布局,如果没有布局,显示不了任何东西
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
//集合视图如果想要显示内容,必须将cell注册 MyCollectionViewCell
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kReuse];
//头部引用的尺寸
layout.headerReferenceSize = CGSizeMake(, );
//如果想要分区头视图显示,必须注册增广视图 MyCollectionReusableView
[collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
//指定代理
collectionView.dataSource = self;
collectionView.delegate = self;
[self.view addSubview:collectionView];
//返回增广视图,也就是头视图
collectionView:viewForSupplementaryElementOfKind:atIndexPath:
注意:
dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
collectionView:cellForItemAtIndexPath:
#pragma mark -----UICollectionViewDelegateFlowLayout---------
collectionView:layout:sizeForItemAtIndexPath:
collectionView:layout:insetForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
自定义UICollectionViewLayout
#import "MyCollectionViewCell.h"
@implementation MyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGFloat totalWidth = frame.size.width;
CGFloat totalHeight = frame.size.height;
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , totalWidth, totalHeight - )];
[self.contentView addSubview:_imageView];
_showLabel = [[UILabel alloc] initWithFrame:CGRectMake(, totalHeight - , totalWidth, )];
_showLabel.textAlignment = NSTextAlignmentCenter;
}
return self;
}
@end
#import "UIImageView+WebCache.h"
#import "Model.h"
#import "MyCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@implementation ViewController
- (void)handleJSon{
//获取json数据的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];
//获取NSData对象
NSData *data = [NSData dataWithContentsOfFile:filePath];
//解析json数据
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
_dataArr = [[NSMutableArray alloc] initWithCapacity:];
for (NSDictionary *dic in arr) {
Model *model = [[Model alloc] init];
[model setValuesForKeysWithDictionary:dic];
[_dataArr addObject:model];
[model release];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
Model *model = _dataArr[indexPath .row];
cell.showLabel.text = [NSString stringWithFormat:@"%ld, %ld",indexPath.section, indexPath.row];
//获取链接
NSURL *url = [NSURL URLWithString:model.thumbURL];
//设置图片链接和占位图片
[cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];
return cell;
}
@end
##DAY15——UICollectionView的更多相关文章
- 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView
开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...
- 使用UICollectionView实现首页的滚动效果
实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...
- 用NSCalendar和UICollectionView自定义日历,并实现签到显示
前一段时间因为工作需要实现了一个可以签到的日历,来记录一下实现的思路 效果如图: 这里的基本需求是: 1,显示用户某个月的签到情况,已经签到的日子打个圈,没有签到且在某个时间范围内的可以签到,其他 ...
- UICollectionView布局cell的三种方式
UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...
- 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cn ...
- UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》
重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...
- UICollectionView中使用 UICollectionViewFlowLayout进行布局(模仿苹果相册)
现在都知道,在初始化UICollectionView的时候,必须要传入一Layout对象,进行布局管理.这也是collectionview和tableview的明显区别,通过collectionvie ...
- UI第十九节——UICollectionView
UICollectionView其实就是UITableView的升级版,在布局方面比UITableView更出色.下面,先看代码吧 #import "RootViewController.h ...
- iOS6新特征:UICollectionView介绍
http://blog.csdn.net/eqera/article/details/8134986 1.1. Collection View 全家福: UICollectionView, UITab ...
随机推荐
- 文件上传与下载/Mail
文件上传与下载 提交方式:post 表单中要有文件上传的表单项 input type=”file”而且必须有name属性 表单类型要加入 encytype=”mulitpart/form-data” ...
- hdu 2768
求最大留下的观众,观众之间存在不能同时满足的关系,就是矛盾关系, 矛盾关系建边,建边是双向的所以最大匹配要/2 还有一种建图的方法:把观众分成两个集合,一个是投留下猫的,一个是投留下狗的 每个集合间没 ...
- Spire PDF for .NET 在ASP.NET中的使用 ---- 并非那么“美好”,有些挫折!
笔者注:看此文前,请您先看一下上一篇文章吧. 昨天的时候,我测试了一下Spire PDF在WinForm程序中的应用,可以说用起来很简单(请忽略效率问题,没有进行测试).不过在互联网如此发达的今天,适 ...
- 网络分析shell脚本(实时流量+连接统计)
介绍一个强大的分析网络的shell脚本,此脚本是从EZHTTP拆分出来的,觉得有必要单独介绍下.脚本运行效果截图: 此脚本包含的功能有: 1.实时监控任意网卡的流量 2.统计10秒内平均流量 3.统计 ...
- 相见恨晚——MarkDown
什么是MarkDown MarkDown是一种轻量级的标记语言 MarkDown使你更加关注文章的内容 MarkDown使文章的排版变得简单直接 什么情景下使用MarkDown 在我们熟悉的githu ...
- BZOJ 1416: [NOI2006]神奇的口袋( 高精度 )
把x1~xn当成是1~n, 答案是不会变的. 然后直接模拟就行了...... P.S 双倍经验... BZOJ1416 && BZOJ1498 -------------------- ...
- mac使用小技
xcodeブラックスクリーンの解決策: 1.cd ~/Library/Developer/Xcode/DerivedData 2.rm -fr * //注释:-fr和*是分开的3.关闭模拟器,关 ...
- linux下flashplayer更新_最新版传送
http://get.adobe.com/cn/flashplayer/ 官网地址,全部最新的flash都在这了,左边选择你要下载的类型,右边点击下载就行了. 最简单的方法就是 下载rpm包,命令rp ...
- windbg命令学习4
4.查看调用栈 k命令:显示的是一定数量的栈帧, 其中帧的数量是由.kframes命令来控制的, 默认值是256. 我们如何来判断函数的栈指针,参数地址和局部变量地址呢? 举一个简单的windbg的k ...
- php命名空间使用
对于命名空间,官方文档已经说得很详细[查看],我在这里做了一下实践和总结. 命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只 ...