#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. url重写步骤

    url重写:在global文件中,在application_BeginRequest事件中1:获取URL string url=Request.AppRelativeCurrentExecutionF ...

  2. CDONTS组件

    在ASP中发送Email时往往需要一个COM组件支持,如果你没有第三方的Email组件,你可以使用IIS本身提供CDONTS EMail组件.这个组件使用时需要安装和启动SMTP服务.这个组件的名称为 ...

  3. 【纯欧拉函数】 poj 2407

    #include <cstdio> #include <iostream> using namespace std; int phi(int x) { int t=x; ;i& ...

  4. 每隔一秒自动执行函数(JavaScript)

    http://www.cnblogs.com/xlx0210/archive/2010/03/19/1689497.html 1. setInterval() ——每隔一秒自动执行方法,setInte ...

  5. 两个数组各个数相加或相乘变成一个矩阵求第K大

    input 1<=T<=20 1<=n<=100000,1<=k<=n*n a1 a2 ... an 0<ai<=10000 b1 b2 ... bn ...

  6. 【转】从1到N这N个数中1的出现了多少次?

    给定一个十进制整数N,求出从1到N的所有整数中出现"1"的个数. 例如:N=2,1,2出现了1个"1". N=12,1,2,3,4,5,6,7,8,9,10,1 ...

  7. set -x /set +x(linux)

    Linux 脚本中生成日志 set -x Posted on 2012-07-25 09:44 紫冰龙 阅读(3946) 评论(0) 编辑 收藏 set -x 与 set +x 在liunx脚本中可用 ...

  8. PHP缓存技术的多种方法小结

    这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓 ...

  9. HBase Coprocessor 剖析与编程实践(转载http://www.cnblogs.com/ventlam/archive/2012/10/30/2747024.html)

    HBase Coprocessor 剖析与编程实践 1.起因(Why HBase  Coprocessor) HBase作为列族数据库最经常被人诟病的特性包括:无法轻易建立“二级索引”,难以执行求和. ...

  10. A*算法实现

    A* 算法非常简单.算法维护两个集合:OPEN 集和 CLOSED 集.OPEN 集包含待检测节点.初始状态,OPEN集仅包含一个元素:开始位置.CLOSED集包含已检测节点.初始状态,CLOSED集 ...