自定义UICollectionViewLayout(适用于多个section)
一、自定义layout主要方法
重写系统的- (void)prepareLayout 方法;
其实就是计算每个cell的frame和其它相关属性。
二、在网上看了好多自定义的layout 但是没有多section的,就整了这个……
全部代码:
@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)的更多相关文章
- 自定义UICollectionViewLayout 实现瀑布流
今天研究了一下自定义UICollectionViewLayout. 看了看官方文档,要自定义UICollectionViewLayout,需要创建一个UICollectionViewLayout的子类 ...
- 自定义UICollectionViewLayout之CATransform3D
1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...
- 自定义UICollectionViewLayout并添加UIDynamic - scorpiozj(转)
转载自:http://www.tuicool.com/articles/jM77Vf 自定义UICollectionViewLayout并添加UIDynamic UICollectionVie ...
- 自定义UICollectionViewLayout并添加UIDynamic
大家也可以到这里查看. UICollectionView是iOS6引入的控件,而UIDynamicAnimator是iOS7上新添加的框架.本文主要涵盖3部分: 一是简单概括UICollectionV ...
- iOS 关于自定义UICollectionViewLayout实现复杂布局
UICollectionView的简单介绍 UICollectionView的结构 Cells Supplementary Views 追加视图 (类似Header或者Footer) Decorati ...
- 自定义UICollectionViewLayout 布局实现瀑布流
自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...
- 自定义UICollectionViewLayout之瀑布流
目标效果 因为系统给我们提供的 UICollectionViewFlowLayout 布局类不能实现瀑布流的效果,如果我们想实现 瀑布流 的效果,需要自定义一个 UICollectionViewLay ...
- iOS自定义UICollectionViewLayout之瀑布流
目标效果 因为系统给我们提供的 UICollectionViewFlowLayout 布局类不能实现瀑布流的效果,如果我们想实现 瀑布流 的效果,需要自定义一个 UICollectionViewLay ...
- 自定义UICollectionViewLayout
UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗新星.它和UITableView共享API设计,但也在UITableView上做了一些扩展.UICollectio ...
随机推荐
- 《转》python基础下
转自http://www.cnblogs.com/BeginMan/archive/2013/04/12/3016323.html 一.数字 在看<Python 核心编程>的时候,我就有点 ...
- D3.js比例尺 定量比例尺 之 线性比例尺(v3版本)
定量比例尺 : 数学上有函数的概念,不是编程中所说的函数,如线性函数.指数函数.对数函数等,而指的是一个量随着另一个量的变化而变化.例如有一下线性函数 : y=2x+1该函数在二维坐标系中绘制出来的图 ...
- dubbo jar 配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- java 数组常见操作
1.遍历 依次访问数组中的每个元素 public class ArrayDemo04 { public static void main(String[] args) { int[] arr = { ...
- Eclipse MyBatis generator 1.3.7插件的核心包(中文注释)
一.最近刚搭建一个项目框架,使用springboot + mybatis,但是在使用Eclipse开发时发现开发mybatis的Dao.mapper.xml和entity时特别不方便,手工去写肯定是不 ...
- 概率期望——cf round362 div1
给定n个数,求i的位置的期望 那么反向考虑,j!=i排在i前面的概率是0.5,那么对i的位置的期望贡献就是1*0.5 这题就是拓展应用一下 #include<bits/stdc++.h> ...
- flutter中的BuildContext
https://www.jianshu.com/p/509b77b26b78
- idea社区版+第一个spring boot项目+增删改查+yml修改端口号
参考:https://www.cnblogs.com/tanlei-sxs/p/9855071.html 中途出现问题时参考了太多 1.下载idea社区版 2.在settings -> Plug ...
- PAT甲级——A1138 Postorder Traversa【25】
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- KVM热添加技术
1.KVM热添加的种类 硬盘.网卡.内存.cpu 2.KVM热添加硬盘 1.在宿主机上创建硬盘 qemu-img create -f qcow2 /opt/web02_add01.qcow2 10G ...