一、自定义layout主要方法
重写系统的- (void)prepareLayout 方法;
其实就是计算每个cell的frame和其它相关属性。
二、在网上看了好多自定义的layout 但是没有多section的,就整了这个……
全部代码:
.h文件
@class ForeverGuardFlowLayout;@protocol ForeverGuardFlowLayoutDelegate- (CGSize)collectionView:(UICollectionView *)collectionView layout:(ForeverGuardFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;@end@interface ForeverGuardFlowLayout : UICollectionViewFlowLayout@property(nonatomic, assign)NSUInteger numberOfColumn;//列数@property(nonatomic, assign)id delegate;@end
1 @interface ForeverGuardFlowLayout() 2 //存放每一列的高度 3 @property (nonatomic, retain) NSMutableArray *columnHeightsArray; 4 5 //每个section的每一列的高度 6 @property (nonatomic, retain) NSMutableArray *collectionHeightsArray; 7 8 //存放每一个cell的属性 9 @property (nonatomic, retain) NSMutableArray *attributesArray; 10 11 12 @end 13 @implementation ForeverGuardFlowLayout 14 15 16 /** 17 每个区的最小高度 18 19 @param section 区索引 20 @return 高度 21 */ 22 - (CGFloat)minHeightWithSection:(NSInteger)section{ 23 CGFloat min = 1000000; 24 for (NSNumber *height in _collectionHeightsArray[section]) { 25 if (min > [height floatValue]) { 26 min = [height floatValue]; 27 } 28 } 29 return min; 30 } 31 32 33 /** 34 每个区的初始Y坐标 35 36 @param section 区索引 37 @return Y坐标 38 */ 39 - (CGFloat)maxHeightWithSection:(NSInteger)section{ 40 41 if (section>0) { 42 CGFloat max = 0; 43 for (int i=0; i*)layoutAttributesForElementsInRect:(CGRect)rect{182 return _attributesArray;183 }184 @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 = 1000000; 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>0) { CGFloat max = 0; for (int i=0; i*)layoutAttributesForElementsInRect:(CGRect)rect{ return _attributesArray;}@end