用CHTCollectionViewWaterfallLayout写瀑布流

实现的瀑布流效果图:

源码:

WaterfallCell.h 与 WaterfallCell.m

//
// WaterfallCell.h
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface WaterfallCell : UICollectionViewCell @property (nonatomic, strong) UIImageView *showImageView; @end
//
// WaterfallCell.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "WaterfallCell.h" @implementation WaterfallCell - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Scale the imageview to fit inside the contentView with the image centered:
CGRect imageViewFrame = CGRectMake(.f, .f,
CGRectGetMaxX(self.contentView.bounds),
CGRectGetMaxY(self.contentView.bounds));
_showImageView = [UIImageView new];
_showImageView.contentMode = UIViewContentModeScaleAspectFill;
_showImageView.frame = imageViewFrame;
_showImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_showImageView.clipsToBounds = YES;
[self addSubview:_showImageView];
self.layer.borderWidth = .f;
}
return self;
} @end

HeaderView.h 与 HeaderView.m

//
// HeaderView.h
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface HeaderView : UICollectionReusableView @end
//
// HeaderView.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "HeaderView.h" @implementation HeaderView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = .f;
}
return self;
} @end

FooterView.h 与 FooterView.m

//
// FooterView.h
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface FooterView : UICollectionReusableView @end
//
// FooterView.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "FooterView.h" @implementation FooterView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.borderWidth = .f;
}
return self;
} @end

使用时候的代码:

//
// RootViewController.m
// UICollectionView
//
// Created by YouXianMing on 14-9-17.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "RootViewController.h"
#import "CHTCollectionViewWaterfallLayout.h" #import "WaterfallCell.h"
#import "HeaderView.h"
#import "FooterView.h" #define CELL_IDENTIFIER @"WaterfallCell"
#define HEADER_IDENTIFIER @"WaterfallHeader"
#define FOOTER_IDENTIFIER @"WaterfallFooter" @interface RootViewController ()<UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout> @property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *dataSourceSize; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 数据源
_dataSource = [NSMutableArray new];
for (int i = ; i <= ; i++)
{
[_dataSource addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]]];
} // 初始化布局
CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init]; // 设置布局
layout.sectionInset = UIEdgeInsetsMake(, , , );
layout.headerHeight = ; // headerView高度
layout.footerHeight = ; // footerView高度
layout.columnCount = ; // 几列显示
layout.minimumColumnSpacing = ; // cell之间的水平间距
layout.minimumInteritemSpacing = ; // cell之间的垂直间距 // 初始化collectionView
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
collectionViewLayout:layout];
_collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor whiteColor]; // 注册cell以及HeaderView,FooterView
[_collectionView registerClass:[WaterfallCell class]
forCellWithReuseIdentifier:CELL_IDENTIFIER];
[_collectionView registerClass:[HeaderView class]
forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
withReuseIdentifier:HEADER_IDENTIFIER];
[_collectionView registerClass:[FooterView class]
forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
withReuseIdentifier:FOOTER_IDENTIFIER]; // 添加到视图当中
[self.view addSubview:_collectionView];
} #pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"您点击了 %@", _dataSource[indexPath.row]);
} #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// 数据源
return [_dataSource count];
} - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
// 1个区
return ;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 注册collectionViewCell
WaterfallCell *cell = \
(WaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER
forIndexPath:indexPath]; UIImage *image = _dataSource[indexPath.row];
cell.showImageView.image = image; return cell;
} - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableView = nil; if ([kind isEqualToString:CHTCollectionElementKindSectionHeader])
{
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:HEADER_IDENTIFIER
forIndexPath:indexPath];
} else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter])
{
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:FOOTER_IDENTIFIER
forIndexPath:indexPath];
} return reusableView;
} #pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 用以返回尺寸
UIImage *image = _dataSource[indexPath.row];
return image.size;
} @end

这个代码再怎么简单也会很复杂-_-!!

以下是使用的细节需要注意的地方:

设置的对应关系-

cell中的图片可不是随便设置的-

要注意返回每个cell的size值-

用CHTCollectionViewWaterfallLayout写瀑布流的更多相关文章

  1. bootstrap+masonry.js写瀑布流

    最近在用bootstrap写一个网站,其中有个图文展示的页面要用到瀑布流的效果.因为项目要求,项目要以bootstrap为基准,不准私自添加内联样式.内部样式,所以,自己写瀑布流就不行了,所以,根据要 ...

  2. 分享:纯 css 瀑布流 和 js 瀑布流

    分享一次纯 css 瀑布流  和 js 瀑布流 纯 css 写瀑布流 1.multi-columns 方式: 通过 Multi-columns 相关的属性 column-count.column-ga ...

  3. 关于瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)

    瀑布流 又称瀑布流式布局,是比较流行的一种网站页面布局方式.即多行等宽元素排列,后面的元素依次添加到其后,等宽不等高,根据图片原比例缩放直至宽度达到我们的要求,依次按照规则放入指定位置. 为什么使用瀑 ...

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

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

  5. 用vue.js写的一个瀑布流的组件

    用vue.js写的一个瀑布流的组件:https://segmentfault.com/a/1190000010741319 https://www.jianshu.com/p/db3cadc03402

  6. 瀑布流封装(仿写UITableView)

    本篇文章将会仿照苹果系统提供的UITableView类,封装一个瀑布流效果的控件!!! 该控件和系统的UITableView是相同级别的 (继承自系统的UIScrollView) GitHub中Dem ...

  7. jquery瀑布流的制作

    首先,还是来看一下炫酷的页面: 今天就边做边说了: 一.准备工作 新建css,js,img文件夹存放相应文件,并在demo.html文件中引入外部文件(注意要把jquery文件引入),这里就不过多描述 ...

  8. CollectionView水平和竖直瀑布流的实现

    最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...

  9. 用jquery实现瀑布流案例

    一.瀑布流是我们常见的案例,这里主要讲述,用jquery的方式实现瀑布流的功能! 引言:我们经常见到很多网站的瀑布流功能,如淘宝.京东这些商品等等.. 实现它我们首先考虑几个问题:1.获取到数据   ...

随机推荐

  1. Python函数中的可变参数

    在Python函数中,还可以定义可变参数. 如:给定一组数字a,b,c……,请计算a2 + b2 + c2 + ……. 要定义出这个函数,我们必须确定输入的参数.由于参数个数不确定,我们首先想到可以把 ...

  2. PHP在使用正则表达式验证,防注入的时候要注意一下的细节

    如下:这是一个防止用户输入的数据中包含SQL的一些关键字的正则表达式 之前一直认为这写的很正确,没多大的问题,而且自己测试也没问题, 因为关键字包含 And,而如果用户输入andy的时候呢,汗,所以还 ...

  3. Pipenv——最好用的python虚拟环境和包管理工具

    pipenv 是Kenneth Reitz大神的作品,能够有效管理Python多个环境,各种包.过去我们一般用virtualenv搭建虚拟环境,管理python版本,但是跨平台的使用不太一致,且有时候 ...

  4. MicrosoftOfficeProfessionalPlus2013激活方法

    MicrosoftOfficeProfessionalPlus2013已经使用很久(估计快一年了吧),但一直是未激活状态,每次打开都要弹出那个未激活的提示信息,很烦,但不知道自己怎么能够忍受这么久的, ...

  5. 百度地图VUE-REACT

    针对目前火热的前端开发框架React和VUE,为了方便使用这两种框架开发的同学们能更好的使用百度地图JSAPI,我们分别开源了基于百度地图JSAPI的React组件库和VUE组件库.VUE:https ...

  6. Android源码博客目录

    每次都找不到,干脆每个部分都开个目录,方便找 0. 杂项 一些Android的博客,没事翻翻 1. 构建相关 linux和Android的Makefile和android.mk android 目录下 ...

  7. c#基础学习(0719)之异常处理

    异常处理的一般代码模式 try { //可能发生异常的代码 //当try中某行代码发生异常后,从该行代码开始,后面的代码都不会继续执行, //程序直接跳转到catch块中进行执行 } catch (E ...

  8. java编写带头结点的单链表

    最近在牛客网上练习在线编程,希望自己坚持下去,每天都坚持下去练习,给自己一个沉淀,不多说了 我遇到了一个用java实现单链表的题目,就自己在做题中将单链表完善了一下,希望大家作为参考也熟悉一下,自己 ...

  9. 使用IDEA工具配置和运行vue项目(详细其中的坑)

    刚来公司实习发现公司的前端使用的是vue,之前根本就没有听说过.然后一上来就需要看代码,but but 就是没有文档什么的东西, 就需要自己去研读,我就想去运行其中的前端和后端联调起来方便理解,结果在 ...

  10. SpringMVC的controller层接收来自jsp页面通过<a href="/user/userUpdateInfo/>的中文乱码问题

    这种情况是,jsp页面的中文正常显示,数据的中文也是正常显示,但是在Controller层接收到的中文是乱码,如下图所示: 解决方法:在Controller层对前台传递的中文乱码进行处理,将它转换成u ...