MyLayout.h文件中
#import <UIKit/UIKit.h>
@class MyLayout;
@protocol MyLayoutDelegate <NSObject>
- (CGFloat)flowLayout:(MyLayout *)flowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath;
@end
@interface MyLayout : UICollectionViewLayout
@property(assign)CGFloat columnMargin;
@property(assign)CGFloat rowMargin;
@property(assign)int columnsCount;
@property (assign) UIEdgeInsets sectionInset;
@property (retain) id<MyLayoutDelegate> delegate;
MyLayout.m文件中
#import "MyLayout.h"
@interface MyLayout ()
@property (nonatomic, strong) NSMutableDictionary *maxYDict;
@property(nonatomic,strong)NSMutableArray *attributeArray;
@end
@implementation MyLayout
-(NSMutableDictionary*)maxYDict
{
if (_maxYDict==nil) {
_maxYDict=[[NSMutableDictionary alloc]init];
}
return _maxYDict;
}
-(NSMutableArray *)attributeArray
{
if (_attributeArray==nil) {
_attributeArray=[[NSMutableArray alloc]init];
}
return _attributeArray;
}
-(id)init
{
self=[super init];
if (self) {
self.columnMargin=15;
self.rowMargin=10;
self.columnsCount=2;
self.sectionInset=UIEdgeInsetsMake(0, 0, 0, 0);
}
return self;
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
-(UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin) / self.columnsCount;
CGFloat height = [self.delegate flowLayout:self heightForWidth:width atIndexPath:indexPath];
__block NSString *minColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {
minColumn = column;
}
}];
CGFloat x = self.sectionInset.left + (self.columnMargin + width) * [minColumn intValue];
CGFloat y = [self.maxYDict[minColumn] floatValue]+ _rowMargin;
self.maxYDict[minColumn] = @(y + height);
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attrs.frame = CGRectMake(x, y, width, height);
return attrs;
}
- (void)prepareLayout
{
[super prepareLayout];
for (int i = 0; i<self.columnsCount; i++) {
NSString *column = [NSString stringWithFormat:@"%d", i];
self.maxYDict[column] = @(self.sectionInset.top);
}
[self.attributeArray removeAllObjects];
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i <count; i++) {
UICollectionViewLayoutAttributes *attris = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[self.attributeArray addObject:attris];
}
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attributeArray;
}
- (CGSize)collectionViewContentSize
{
__block NSString *maxColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {
maxColumn = column;
}
}];
return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom);
}
@end
MyCollectionViewCell。h文件中
#import <UIKit/UIKit.h>
@class MyCollectionViewCell;
@interface MyCollectionViewCell : UICollectionViewCell
@property(retain)UILabel* label;
@property(retain)UIImageView* imageViews;
@end
MyCollectionViewCell.m文件中
#import "MyCollectionViewCell.h"
#define width [UIScreen mainScreen].bounds.size.width
#define height [UIScreen mainScreen].bounds.size.height
@implementation MyCollectionViewCell
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
_imageViews=[[UIImageView alloc]initWithFrame:CGRectZero];
[self.contentView addSubview:_imageViews];
_label=[[UILabel alloc]initWithFrame:CGRectZero];
[self.contentView addSubview:_label];
}
return self;
}
#pragma mark - 重写方法 进行重新布局视图
- (void)layoutSubviews
{
[super layoutSubviews];
}
@end
ViewController.m文件中
#import "ViewController.h"
#import "MyLayout.h"
#import "MyCollectionViewCell.h"
#define width1 [UIScreen mainScreen].bounds.size.width
#define height1 [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,MyLayoutDelegate>
@property(retain)NSMutableArray* dataSoucers;
@property(assign)NSInteger number;
@property(retain)UICollectionView* collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dataSoucers=[[NSMutableArray alloc]init];
self.automaticallyAdjustsScrollViewInsets=YES;
self.view.backgroundColor=[UIColor lightGrayColor];
for (int i=0; i<23; i++) {
NSString* str=[NSString stringWithFormat:@"第:%d 张",i+1];
[_dataSoucers addObject:str];
}
MyLayout* layout=[[MyLayout alloc]init];
layout.delegate=self;
_collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 20,width1 , height1-20) collectionViewLayout:layout];
_collectionView.delegate=self;
_collectionView.dataSource=self;
_collectionView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:_collectionView];
[_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _dataSoucers.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
if (indexPath.row%2==0) {
cell.backgroundColor=[UIColor greenColor];
cell.imageViews.image=[UIImage imageNamed:@"32_2"];
cell.imageViews.frame=CGRectMake(0, 0, (width1-15)/2, 120);
cell.label.frame=CGRectMake(0, 120, (width1-15)/2, 30);
}
else {
cell.backgroundColor=[UIColor cyanColor];
cell.imageViews.image=[UIImage imageNamed:@"32_8"];
cell.imageViews.frame=CGRectMake(0, 0, (width1-15)/2, 170);
cell.label.frame=CGRectMake(0, 170, (width1-15)/2, 30);
}
cell.label.text=_dataSoucers[indexPath.row];
cell.label.textAlignment=NSTextAlignmentCenter;
cell.label.font=[UIFont boldSystemFontOfSize:20];
return cell;
}
- (CGFloat)flowLayout:(MyLayout *)flowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row%2==0) {
return 150;
}
else
return 200;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
![这里写图片描述](https://img-blog.csdn.net/20170630111612662?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmlhblJlbl9M/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)