一、自定义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. javascript字符串方法学习汇总

    1.charAt(index) charAt(index):返回字符串中指定位置的字符 var str = 'abcdefghi'; console.log(str.charAt()); // 输出 ...

  2. wish - 简单的窗口式(windowing) shell

    总览 wish [filename] [arg] [arg ...] 选项 -colormap new 指定窗口使用一个新的私有的调色板(colormap)而不使用给屏幕的缺省的调色板. -displ ...

  3. java Twain 直接打印/界面打印

    这两天,在搞归档系统.需要用到Twain协议来驱动扫描仪. 找了两天,java的twain操作资料真的不多.而且我还是要找直接打印的功能. 后来只能静下心来看类库和源码.最后搞定他. 打印方式分为3种 ...

  4. mysql 远程连接报错

    ./mysql -uzhu -p -h192.168.1.200 ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.200 ...

  5. django静态文件的配置

    Django static静态配置文件   对于Django来说静态文件一般交由Web服务器处理,Django本身不处理静态文件.为了使Django开发环境能够处理静态文件,Django有和生产环境不 ...

  6. delphi 第3课

    (1)主程序:汇总或者记载 Delphi应用程序是以窗体为中心的 (1) 1 (2) 控制语句 if 条件 then 语句1: else 语句2: 2018-04-22   21:47:17

  7. Hadoop配置lzo

    编译: 0. 环境准备 maven(下载安装,配置环境变量,修改sitting.xml加阿里云镜像) gcc-c++ zlib-devel autoconf automake libtool 通过yu ...

  8. 最大流——poj1459

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  9. WannaCry结束了? 安专家注册域名掐断病毒传播

    腾讯科技讯,(韩依民) 5 月 13 日,席卷全球的勒索病毒 WannaCry(也被称作 WanaCrypt 或 WCry),在今日晚间被互联网安全人员找到阻止其传播的方法. 据北京云纵信息技术有限公 ...

  10. PAT甲级——A1111 Online Map【30】

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...