一、自定义layout主要方法

  重写系统的- (void)prepareLayout  方法;

  其实就是计算每个cell的frame和其它相关属性。

  

二、在网上看了好多自定义的layout 但是没有多section的,就整了这个……

  全部代码:

.h文件

@class ForeverGuardFlowLayout;
@protocol ForeverGuardFlowLayoutDelegate <NSObject>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(ForeverGuardFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
@end @interface ForeverGuardFlowLayout : UICollectionViewFlowLayout @property(nonatomic, assign)NSUInteger numberOfColumn;//列数
@property(nonatomic, assign)id<ForeverGuardFlowLayoutDelegate>delegate; @end
 @interface ForeverGuardFlowLayout()
//存放每一列的高度
@property (nonatomic, retain) NSMutableArray *columnHeightsArray; //每个section的每一列的高度
@property (nonatomic, retain) NSMutableArray *collectionHeightsArray; //存放每一个cell的属性
@property (nonatomic, retain) NSMutableArray *attributesArray; @end
@implementation ForeverGuardFlowLayout /**
每个区的最小高度 @param section 区索引
@return 高度
*/
- (CGFloat)minHeightWithSection:(NSInteger)section{
CGFloat min = ;
for (NSNumber *height in _collectionHeightsArray[section]) {
if (min > [height floatValue]) {
min = [height floatValue];
}
}
return min;
} /**
每个区的初始Y坐标 @param section 区索引
@return Y坐标
*/
- (CGFloat)maxHeightWithSection:(NSInteger)section{ if (section>) {
CGFloat max = ;
for (int i=; i<section; i++) {
max += [self maxHeightAboutSection:_collectionHeightsArray[i]];
}
return max;
}else{
return ;
} } /**
每个区的最大高度 @param dataArr 每个区的所有列的高度
@return 最大高度
*/
- (CGFloat)maxHeightAboutSection:(NSMutableArray *)dataArr{
CGFloat max = ;
for (NSNumber *heigth in dataArr) {
if (max < [heigth floatValue]) {
max = [heigth floatValue];
}
}
return max; } /**
collectionView的显示高度
*/
- (CGFloat)collectionHeight{
CGFloat max = ;
for (NSMutableArray *sectionArr in _collectionHeightsArray) {
max += [self maxHeightAboutSection:sectionArr];
}
return max;
} /**
当前区的最小高度的索引 @param section 当前区
@return 最小高度的索引
*/
- (NSUInteger)indexOfMinHeightWithSection:(NSInteger)section{
NSUInteger index = ;
NSMutableArray *sectionArr = _collectionHeightsArray[section];
for (int i=; i<sectionArr.count; i++) {
CGFloat height = [sectionArr[i] floatValue];
if (height == [self minHeightWithSection:section]) {
index = i;
return index;
}
}
return index;
} /**
重写系统prepareLayout方法 (设置item的坐标等属性)
*/
- (void)prepareLayout{
[super prepareLayout]; _attributesArray = [[NSMutableArray alloc] init]; _columnHeightsArray = [NSMutableArray arrayWithCapacity:self.numberOfColumn]; NSUInteger sectionCount = [self.collectionView numberOfSections];
_collectionHeightsArray = [NSMutableArray arrayWithCapacity:sectionCount]; for (int index = ; index<sectionCount; index++) {
NSMutableArray *columnArr = [NSMutableArray array];
for (int i=; i<self.numberOfColumn; i++) {
[columnArr addObject:@0.0];
}
[_collectionHeightsArray addObject:columnArr];
} CGFloat totalWidth = self.collectionView.frame.size.width; CGFloat x = ;
CGFloat y = ; for (int index= ; index<sectionCount; index++) {
NSUInteger itemCount = [self.collectionView numberOfItemsInSection:index];
x = ;
y = [self maxHeightWithSection:index];
for (int i=; i<itemCount; i++) {
NSUInteger numberIfSoace = self.numberOfColumn-;
CGFloat spaceWidth = 7.5;//item左右间距
CGFloat width = (totalWidth - spaceWidth*numberIfSoace)/self.numberOfColumn; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:index]; CGSize imageSize =[self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath]; CGFloat height = width * imageSize.height / imageSize.width; UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attributes.frame = CGRectMake(x, y, width, height);
[_attributesArray addObject:attributes]; NSUInteger minHeightIndex = [self indexOfMinHeightWithSection:index]; CGFloat minHeight = [_collectionHeightsArray[index][minHeightIndex] floatValue];
CGFloat lineHeight = 7.5;//item上下间距 _collectionHeightsArray[index][minHeightIndex] = [NSNumber numberWithFloat:minHeight+lineHeight+height];
minHeightIndex = [self indexOfMinHeightWithSection:index]; x = (spaceWidth + width) * minHeightIndex; y += [self minHeightWithSection:index]; } } } /**
返回collectionView的界面显示大小
*/
- (CGSize)collectionViewContentSize{
return CGSizeMake(self.collectionView.frame.size.width, [self collectionHeight]);
} /**
将所有的layoutAttributes重写布局
*/
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return _attributesArray;
}
@end

.m文件

@interface ForeverGuardFlowLayout()
//存放每一列的高度
@property (nonatomic, retain) NSMutableArray *columnHeightsArray;
//每个section的每一列的高度
@property (nonatomic, retain) NSMutableArray *collectionHeightsArray;
//存放每一个cell的属性
@property (nonatomic, retain) NSMutableArray *attributesArray; @end
@implementation ForeverGuardFlowLayout /**
每个区的最小高度 @param section 区索引
@return 高度
*/
- (CGFloat)minHeightWithSection:(NSInteger)section{
CGFloat min = ;
for (NSNumber *height in _collectionHeightsArray[section]) {
if (min > [height floatValue]) {
min = [height floatValue];
}
}
return min;
} /**
每个区的初始Y坐标 @param section 区索引
@return Y坐标
*/
- (CGFloat)maxHeightWithSection:(NSInteger)section{ if (section>) {
CGFloat max = ;
for (int i=; i<section; i++) {
max += [self maxHeightAboutSection:_collectionHeightsArray[i]];
}
return max;
}else{
return ;
} } /**
每个区的最大高度 @param dataArr 每个区的所有列的高度
@return 最大高度
*/
- (CGFloat)maxHeightAboutSection:(NSMutableArray *)dataArr{
CGFloat max = ;
for (NSNumber *heigth in dataArr) {
if (max < [heigth floatValue]) {
max = [heigth floatValue];
}
}
return max; } /**
collectionView的显示高度
*/
- (CGFloat)collectionHeight{
CGFloat max = ;
for (NSMutableArray *sectionArr in _collectionHeightsArray) {
max += [self maxHeightAboutSection:sectionArr];
}
return max;
} /**
当前区的最小高度的索引 @param section 当前区
@return 最小高度的索引
*/
- (NSUInteger)indexOfMinHeightWithSection:(NSInteger)section{
NSUInteger index = ;
NSMutableArray *sectionArr = _collectionHeightsArray[section];
for (int i=; i<sectionArr.count; i++) {
CGFloat height = [sectionArr[i] floatValue];
if (height == [self minHeightWithSection:section]) {
index = i;
return index;
}
}
return index;
} /**
重写系统prepareLayout方法 (设置item的坐标等属性)
*/
- (void)prepareLayout{
[super prepareLayout];
_attributesArray = [[NSMutableArray alloc] init];
_columnHeightsArray = [NSMutableArray arrayWithCapacity:self.numberOfColumn];
NSUInteger sectionCount = [self.collectionView numberOfSections];
_collectionHeightsArray = [NSMutableArray arrayWithCapacity:sectionCount];
for (int index = ; index<sectionCount; index++) {
NSMutableArray *columnArr = [NSMutableArray array];
for (int i=; i<self.numberOfColumn; i++) {
[columnArr addObject:@0.0];
}
[_collectionHeightsArray addObject:columnArr];
}
CGFloat totalWidth = self.collectionView.frame.size.width; CGFloat x = ;
CGFloat y = ; for (int index= ; index<sectionCount; index++) {
NSUInteger itemCount = [self.collectionView numberOfItemsInSection:index];
x = ;
y = [self maxHeightWithSection:index];
for (int i=; i<itemCount; i++) {
NSUInteger numberIfSoace = self.numberOfColumn-;
CGFloat spaceWidth = 7.5;//item左右间距
CGFloat width = (totalWidth - spaceWidth*numberIfSoace)/self.numberOfColumn;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:index];
CGSize imageSize =[self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
CGFloat height = width * imageSize.height / imageSize.width;
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(x, y, width, height);
[_attributesArray addObject:attributes]; NSUInteger minHeightIndex = [self indexOfMinHeightWithSection:index];
CGFloat minHeight = [_collectionHeightsArray[index][minHeightIndex] floatValue];
CGFloat lineHeight = 7.5;//item上下间距 _collectionHeightsArray[index][minHeightIndex] = [NSNumber numberWithFloat:minHeight+lineHeight+height];
minHeightIndex = [self indexOfMinHeightWithSection:index]; x = (spaceWidth + width) * minHeightIndex;
y = [self minHeightWithSection:index]+[self maxHeightWithSection:index];
}
}
} /**
返回collectionView的界面显示大小
*/
- (CGSize)collectionViewContentSize{
return CGSizeMake(self.collectionView.frame.size.width, [self collectionHeight]);
} /**
将所有的layoutAttributes重写布局
*/
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return _attributesArray;
}
@end



自定义UICollectionViewLayout(适用于多个section)的更多相关文章

  1. 自定义UICollectionViewLayout 实现瀑布流

    今天研究了一下自定义UICollectionViewLayout. 看了看官方文档,要自定义UICollectionViewLayout,需要创建一个UICollectionViewLayout的子类 ...

  2. 自定义UICollectionViewLayout之CATransform3D

    1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...

  3. 自定义UICollectionViewLayout并添加UIDynamic - scorpiozj(转)

    转载自:http://www.tuicool.com/articles/jM77Vf     自定义UICollectionViewLayout并添加UIDynamic UICollectionVie ...

  4. 自定义UICollectionViewLayout并添加UIDynamic

    大家也可以到这里查看. UICollectionView是iOS6引入的控件,而UIDynamicAnimator是iOS7上新添加的框架.本文主要涵盖3部分: 一是简单概括UICollectionV ...

  5. iOS 关于自定义UICollectionViewLayout实现复杂布局

    UICollectionView的简单介绍 UICollectionView的结构 Cells Supplementary Views 追加视图 (类似Header或者Footer) Decorati ...

  6. 自定义UICollectionViewLayout 布局实现瀑布流

    自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...

  7. 自定义UICollectionViewLayout之瀑布流

    目标效果 因为系统给我们提供的 UICollectionViewFlowLayout 布局类不能实现瀑布流的效果,如果我们想实现 瀑布流 的效果,需要自定义一个 UICollectionViewLay ...

  8. iOS自定义UICollectionViewLayout之瀑布流

    目标效果 因为系统给我们提供的 UICollectionViewFlowLayout 布局类不能实现瀑布流的效果,如果我们想实现 瀑布流 的效果,需要自定义一个 UICollectionViewLay ...

  9. 自定义UICollectionViewLayout

    UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗新星.它和UITableView共享API设计,但也在UITableView上做了一些扩展.UICollectio ...

随机推荐

  1. 2.1_springboot2.x消息介绍&RabbitMQ运行机制

    1.概述 1.大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2.消息服务中两个重要概念: ​ 消息代理(message broker)即消息服务器 和目的地(destination ...

  2. PyTorch中,关于model.eval()和torch.no_grad()

    一直对于model.eval()和torch.no_grad()有些疑惑 之前看博客说,只用torch.no_grad()即可 但是今天查资料,发现不是这样,而是两者都用,因为两者有着不同的作用 引用 ...

  3. JavaScript——问卷星自动填写

    一.前言: 我们学校要刷学术章,有些学术章又是指定在某个时间点填写问卷星的问卷报名的.但是由于我手速慢,导致总会有些时候报不上名,于是想着搞个代码实现自动填写问卷星的报名表.一顿操作后,在github ...

  4. 2019-8-31-dotnet-判断程序当前使用管理员运行降低权使用普通权限运行

    title author date CreateTime categories dotnet 判断程序当前使用管理员运行降低权使用普通权限运行 lindexi 2019-08-31 16:55:58 ...

  5. vue v-model :

    v-model : 通过v-model 进行双向绑定 ,将data的数据与input 绑定在一起,呈现在页面上 <!DOCTYPE html> <html lang="en ...

  6. Android开发 MediaPlayer播放raw资源MP3文件

    代码 private MediaPlayer mRingPlayer; /** * 播放铃声 */ private void startRing(){ if (mRingPlayer != null) ...

  7. ipsec原理(转载)

    IPSec VPN是目前VPN技术中点击率非常高的一种技术,同时提供VPN和信息加密两项技术,这一期专栏就来介绍一下IPSec VPN的原理.IPSec VPN应用场景 IPSec VPN的应用场景分 ...

  8. 【JZOJ6354】最短路(tiring)

    description analysis 显然边权有变化规律\(x,{1\over{x-1}},{x-1\over x},x,...\) 于是把一个点拆成三个点,分别表示步数到除\(3\)余\(0,1 ...

  9. C/C++ 公有函数无法返回私有的类对象解决方案

    { 能出这种错的说明还需要提升C++,增强对类的理解 解决方案:把你的私有的对象的私有的拷贝构造或者同类赋值改为公开的 }

  10. CSS-基本语法/引用/文本设置/选择器/css3属性

    CSS-基本语法/引用/文本设置 css基本语法及页面引用 css基本语法 css的定义方法是: 选择器 { 属性:值; 属性:值; 属性:值;} 选择器是将样式和页面元素关联起来的名称,属性是希望设 ...