转载自:http://www.cnblogs.com/tmf-4838/p/5361271.html

#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(0, 30); //头部尺寸
layout.footerReferenceSize = CGSizeMake(0, 100); //尾部尺寸
// 缩进:和屏幕上下以及相对于屏幕的左右间距(上左下右):左右会影响到竖直间距
// 默认是10:终于可以改变了
layout.sectionInset = UIEdgeInsetsMake(1, 0, 1, 0);
// 设置图片的尺寸
//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-1, WID/4.0-1);
// 前提minimumInteritemSpacing为0(默认为10)
layout.minimumInteritemSpacing = 0;
// 行间距(竖直模式)
layout.minimumLineSpacing = 1;
// 设置页眉和页脚是否一直存在
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(0, 30);
} #pragma - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
} - (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(0, 0, 100, 15);
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 = 2;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.frame = CGRectMake((WID-200)/2, 35, 200, 30);
lbl.font = [UIFont systemFontOfSize:10]; 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:11]; 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

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

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

    #import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+Web ...

  2. iOS 产品新需求,要让collectionView 的背景跟着Cell 一块儿动!!!

    标题如上!看如何解决 最近产品经理来需求了,就像标题上的一样,要求,给collectionView添加一个背景,并且这个背景,还能跟着cell滑动这么一个效果,这个需求把我看懵了,让我不知所措,这该如 ...

  3. iOS 11 导航栏 item 偏移问题 和 Swift 下 UIButton 设置 title、image 显示问题

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  4. iOS之可拖拽重排的CollectionView

    修复了拖拽滚动时抖动的一个bug,新增编辑模式,进入编辑模式后不用长按触发手势,且在开启抖动的情况下会自动进入抖动模式,如图: test.gif 图1:垂直滚动 drag1.gif 图2:水平滚动 d ...

  5. collectionView 中cell间距设置建议

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

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

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

  7. iOS SDWebImage使用详解

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征. 使用示范的代码:UITableView使用UIImageView ...

  8. IOS - SDWebImage 非ARC 问题

    非arc项目中使用SDWebImage类库 1.添加类库引用    (1)ImageIO.framework    (2)MapKit.framework 2.在targets->build P ...

  9. IOS SDWebImage实现原理详解

    在之前我写过SDWebImage的使用方法,主要是用与获取网络图片,没有看过的朋友可以看看. 这篇文章将主要介绍SDWebImage的实现原理,主要针对于获取网络图片的原理,如果没有第三方我们该怎么去 ...

随机推荐

  1. [UWP小白日记-5]转换MVA学院的XML字幕为SRT

    开源地址:第二版开源地址GIT 暂时用不了了,在最新的WIN10 10586.494系统上回闪退,正在酝酿第二版 O(∩_∩)O哈哈~ 新版已经完工:第二版 地方MVA上好多教程,但是微软的所有中国网 ...

  2. trove,测试,db小解析

    # Copyright 2014 Tesora Inc.# All Rights Reserved.## Licensed under the Apache License, Version 2.0 ...

  3. google 技巧

    inurl: 用于搜索网页上包含的URL. 这个语法对寻找网页上的搜索,帮助之类的很有用. intext: 只搜索网页部分中包含的文字(也就是忽略了标题,URL等的文字). site: 可以限制你搜索 ...

  4. markdown 自定义一个锚点

    //自定义锚点 s "m[": function mlink( text ) { var orig = String(text); // Inline content is pos ...

  5. iosAPP打包上架xcode中Archive提交成功以后,不提示构建版本问题

    最近在项目更新时遇到Archive提交到开发者中心成功后,一直不提示构建版本信息,可能导致的原因是由于ios10以后对于APP中调用手机相册或摄像头麦克风时需要配置plist文件,配置如下内容或许会解 ...

  6. centos php 扩展安装

    1. 安装mysqli扩展 1.进入php源代码目录:# cd /home/apps/web/php/php-5.3.5/ 2.再进入要添加的mysqli扩展源码目录:# cd ext/mysqli/ ...

  7. 最新Node.js 资源汇总

    Node.js 资源汇总 文档 Node.js 官方文档:http://nodejs.org/api/ Node.js 中文文档:http://nodejs.jsbin.cn/api/ Express ...

  8. JQuery 模糊匹配

    [属性名称]         匹配包含给定属性的元素 [att=value]       匹配包含给定属性的元素      [att*=value]     模糊匹配      [att!=value ...

  9. [linux]查看机器有几个cpu,是否支持64位

    1. 查看物理CPU的个数#cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 2. 查看逻辑CPU的个数#cat /pr ...

  10. java中String类型的相关知识

    String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...