#import "ViewController.h"
#import <ImageIO/ImageIO.h>
#import "UIImageView+WebCache.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
// 数据源
@property (nonatomic, strong)NSMutableArray *arrIcon;
// 显示控件
@property (nonatomic, strong)UICollectionView *clv;
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 获取数据源写成,url写成plist文件
/*
[[NSArray arrayWithObjects:
@"http://image.tianjimedia.com/uploadImages/2012/273/M6J97CZGYA4Z_NatGeo01_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/J3ME8ZNAG315_NatGeo02_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/EL9230AP5196_NatGeo03_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/SYJ43SG47PC8_NatGeo04_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/605X52620G0M_NatGeo05_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/7H5RQ1ELP8MZ_NatGeo06_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/Z2W429E0203R_NatGeo07_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/BG011W9LWL77_NatGeo08_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/936FM8NN22J2_NatGeo09_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/H79633PPEFZW_NatGeo10_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/54Z01YZ78050_NatGeo11_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/61V3658UA4IY_NatGeo12_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/H3HL7YILNGKB_NatGeo13_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/1V6797311ZA5_NatGeo14_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/93L81IKN156R_NatGeo15_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/V93E1EGU2G0Z_NatGeo16_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/QC205CD96IWZ_NatGeo17_250.jpg",@"http://image.tianjimedia.com/uploadImages/2012/273/M6J97CZGYA4Z_NatGeo01_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/J3ME8ZNAG315_NatGeo02_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/EL9230AP5196_NatGeo03_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/SYJ43SG47PC8_NatGeo04_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/605X52620G0M_NatGeo05_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/7H5RQ1ELP8MZ_NatGeo06_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/Z2W429E0203R_NatGeo07_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/BG011W9LWL77_NatGeo08_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/936FM8NN22J2_NatGeo09_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/H79633PPEFZW_NatGeo10_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/54Z01YZ78050_NatGeo11_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/61V3658UA4IY_NatGeo12_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/H3HL7YILNGKB_NatGeo13_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/1V6797311ZA5_NatGeo14_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/93L81IKN156R_NatGeo15_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/V93E1EGU2G0Z_NatGeo16_250.jpg",
@"http://image.tianjimedia.com/uploadImages/2012/273/QC205CD96IWZ_NatGeo17_250.jpg",
nil] writeToFile:@"/Users/Mu/Desktop/pic.plist" atomically:YES];
*/ self.arrIcon = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pic" ofType:@"plist"]]; // 设置滚动方向:FlowLayout简单说是一个直线对齐的layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 滚动方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical; // 尾部距离屏幕尺寸(竖直模式,x没有作用)
layout.headerReferenceSize = CGSizeMake(, ); //头部尺寸
layout.footerReferenceSize = CGSizeMake(, ); //尾部尺寸
// 缩进:和屏幕上下以及相对于屏幕的左右间距(上左下右):左右会影响到竖直间距
// 默认是10:终于可以改变了
layout.sectionInset = UIEdgeInsetsMake(, , , );
// 设置图片的尺寸
//layout.itemSize = CGSizeMake(WID/4.0-1, WID/4.0-1);
//layout.itemSize = CGSizeMake((WID-30)/4.0, (WID-30)/4.0);
// 目前发现:预估尺寸等效于于itemSize
layout.estimatedItemSize = CGSizeMake(WID/4.0-, WID/4.0-);
// 前提minimumInteritemSpacing为0(默认为10)
layout.minimumInteritemSpacing = ;
// 行间距(竖直模式)
layout.minimumLineSpacing = ;
// 设置页眉和页脚是否一直存在
layout.sectionHeadersPinToVisibleBounds= NO; // 创建对象
self.clv = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
// 指定代理
self.clv.delegate = self;
self.clv.dataSource = self; // 注册唯一标识
[self.clv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
[self.clv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReuse"];
[self.clv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footReuse"]; // 添加到主视图
[self.view addSubview:self.clv];
self.clv.backgroundColor = [UIColor whiteColor]; // 设置偏移量,隐藏UISegmentedControl
self.clv.contentOffset = CGPointMake(, );
} #pragma - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
} - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
[self.arrIcon addObject:[NSNull null]];
[self.arrIcon addObject:[NSNull null]];
return self.arrIcon.count;
} // 首先是cell本身作为容器view\
然后是一个大小自动适应整个cell的backgroundView,用作cell平时的背景\
再其上是selectedBackgroundView,是cell被选中时的背景\
最后是一个contentView,自定义内容应被加在这个view上
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 重用队列标识
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath]; // 第三方Image请求
UIImageView *imv = [[UIImageView alloc] init];
[imv sd_setImageWithURL:self.arrIcon[indexPath.row] placeholderImage:[UIImage imageNamed:@"u=2312994427,1092373000&fm=21&gp=0.jpg"] options:SDWebImageRetryFailed];
cell.backgroundView = imv; // 选中更换背景图_1(点击选中即更换)
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Img361868872.jpg"]]; return cell;
} // 页眉和页脚出现即调用;页眉要第二次出现才会调用
// Supplementary Views 追加视图 如果你对UITableView比较熟悉的话,可以理解为每个Section的Header或者Footer,用来标记每个section的view
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader)
{
// 重用标识
UICollectionReusableView *ppView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headReuse" forIndexPath:indexPath];
ppView.backgroundColor = [UIColor clearColor];
// 添加子控件
UISegmentedControl *sgmc = [[UISegmentedControl alloc] initWithItems:@[@"名称", @"日期", @"大小"]];
sgmc.tintColor = [UIColor purpleColor];
sgmc.bounds = CGRectMake(, , , );
sgmc.center = ppView.center;
[ppView addSubview:sgmc]; return ppView;
}else
{
// 重用标识
UICollectionReusableView *ppView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footReuse" forIndexPath:indexPath];
ppView.backgroundColor = [UIColor clearColor]; // 添加数量和时间
UILabel *lbl = [[UILabel alloc] init];
lbl.numberOfLines = ;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.frame = CGRectMake((WID-)/, , , );
lbl.font = [UIFont systemFontOfSize:]; NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger seconds = [zone secondsFromGMTForDate:date];
NSDate *now = [date dateByAddingTimeInterval:seconds]; // 格式化NSDate为NSString:会自动添加28800s即等于now
NSDateFormatter *fm = [NSDateFormatter new];
fm.dateFormat = @"yyyy年MM月dd日HH时mm分ss秒"; // 需要取子串
NSString *time = [[fm stringFromDate:date] substringFromIndex:]; NSString *num = [NSString stringWithFormat:@"%lu张照片", self.arrIcon.count];
lbl.text = [NSString stringWithFormat:@"%@\r\n%@", num, time]; [ppView addSubview:lbl];
NSLog(@"等效:%@;%@", now, [fm stringFromDate:date]); return ppView;
} }
// 对于Decoration Views,提供方法并不在UICollectionViewDataSource中,而是直接UICollectionViewLayout类中的(因为它仅仅是视图相关,而与数据无关) // 选中跟换背景图_2(长按更换松开恢复)
// 需要允许为YES,不然走发哦这里就停了不会执行didHighlightItemAtIndexPath
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
// 没有创建新对象:使用的还是indexPath队列的cell
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Img361868872.jpg"]];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
// 没有创建新对象:使用的还是indexPath队列的cell
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; UIImageView *imv = [[UIImageView alloc] init];
[imv sd_setImageWithURL:self.arrIcon[indexPath.row] placeholderImage:[UIImage imageNamed:@"u=2312994427,1092373000&fm=21&gp=0.jpg"] options:SDWebImageRetryFailed];
cell.backgroundView = imv; }UIImageView *imv;
// 选中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%lu", indexPath.row);
}
@end

SDWEBImage和collectionView的组合,以及collectionView的随意间距设置的更多相关文章

  1. iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

    转载自:http://www.cnblogs.com/tmf-4838/p/5361271.html #import "ViewController.h" #import < ...

  2. collectionView 中cell间距设置建议

    应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,这个是调节同一行的cell之间的距离的. 使用-(CGFloat )collecti ...

  3. 如何自定义CollectionView中每个元素的大小和间距

    问题: 让每个元素大小变为104 x 104 Step 1: 在你的视图控制器头文件中实现UICollectionViewFlowLayout协议 eg: @interface XXViewContr ...

  4. collectionView

    // /* UICollectionView 类是iOS6 新引进的API,用于展示集合视图, 布局更加灵活,可实现多列布局,用法类似于UITableView类. - 更新视图: [collectio ...

  5. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

  6. collectionView,tableView的细节处理

    1.设置collectionView的高度 1.1为什么要设置高度? collectionView是在tableView的footView里面 , tableView能滚动,collectionVie ...

  7. iOS9 collectionView新特性

    近日因为系统升级导致xcode6.系列版本出现bug,于是开始使用xcode7.在使用之余突然想到collectionView在iOS9中发布了一个可以移动cell的新特性,就尝试着将其实现,无奈ap ...

  8. 使用 collectionView 实现表头,区头,区尾

    UICollectionView 的使用是跟表的使用是一样,瀑布流的布局会比表的效果更好,这里说一下 collectionView 设置表头, 区头,区尾 设置表头可以约束 collectionVie ...

  9. collectionView代码创建

    @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> @p ...

随机推荐

  1. think in uml-关系

    1.关联关系association 在一段时间内将多个类的实例连接在一起 某个对象在一段时间内一直"知道"另一个对象的存在 2.依赖关系dependency 一个对象的修改会导致另 ...

  2. 【单源最短路模板】 poj 2387

    #include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...

  3. 学习笔记:GLSL Core Tutorial – Vertex Shader(内置变量说明)

    1.每个Vertex Shader都有用户定义的输入属性,例如:位置,法线向量和纹理坐标等.Vertex Shaders也接收一致变量(uniform variables). uniform vari ...

  4. C++ 学习之函数重载、基于const的重载

    函数重载 函数重载的定义是:在相同的作用域中,如果函数具有相同名字而仅仅是形参表不同,此时成为函数重载.注意函数重载不能基于不同的返回值类型进行重载. 注意函数重载中的"形参表"不 ...

  5. ShellExecute快捷键大全

    文件夹,文件,网址可以创建快捷方式,控制面板 中的设置也可以创建快捷方式,下面是快捷方式的命令,使用方法:在桌面或文件夹的空白处点右键,选择新建,快捷方式,在"请键入项目的位置"输 ...

  6. Nape "不动"刚体的碰撞交互

    "不动"刚体,这里指的是以下两类刚体: 1.刚体的allowMovement和allowRotation都设false的刚体 2.KINEMATIC类型的刚体 如下代码,测试发现输 ...

  7. Node.js学习 - Install and Configure

    平台:Windows 官网:https://nodejs.org/en/ 下载安装 CMD中运行 1 交互模式 2 命令模式 模块安装 - NPM npm install express #当前目录安 ...

  8. 一个简单的基于HTTP协议的屏幕共享应用

    HTTP协议可以能是应用层协议里使用最广泛并且用途最多样的一个了.我们一般使用HTTP协议来浏览网页,但是HTTP协议还用来做很多其它用途.对开发人员来讲很常见的一种就是用HTTP协议作为各种版本控制 ...

  9. 业务零影响!如何在Online环境中巧用MySQL传统复制技术【转】

    业务零影响!如何在Online环境中巧用MySQL传统复制技术 这篇文章我并不会介绍如何部署一个MySQL复制环境或keepalived+双主环境,因为此类安装搭建的文章已经很多,大家也很熟悉.在这篇 ...

  10. WiresShark 一站式学习

    按照国际惯例,从最基本的说起. 抓取报文: 下载和安装好Wireshark之后,启动Wireshark并且在接口列表中选择接口名,然后开始在此接口上抓包.例如,如果想要在无线网络上抓取流量,点击无线接 ...