美文网首页
简单实现瀑布流

简单实现瀑布流

作者: dqk1023 | 来源:发表于2016-09-02 15:35 被阅读0次

    首先在viewcontroller 中创建collectionView;自定义一个类继承自UICollectionViewFlowLayout代码实现如下;

    ```Object-C

    viewController .m

    //

    //ViewController.m

    //MYCollectionView

    //

    //Created by dqk on 16/9/2.

    //Copyright © 2016年dqk. All rights reserved.

    //

    #import"ViewController.h"

    #import"MYLayout.h"

    #import"CollectionViewCell.h"

    #define kScreenWidth [UIScreen mainScreen].bounds.size.width

    #define kScreenHeight [UIScreen mainScreen].bounds.size.height

    @interfaceViewController()

    @property(nonatomic,strong)NSMutableArray*arry;

    @end

    staticNSString*constcellresure =@"cell";

    @implementationViewController

    -(NSMutableArray*)arry

    {

    if(_arry==nil)

    {

    _arry=[[NSMutableArrayalloc]init];

    }

    return_arry;

    }

    ```Object-C

    - (void)viewDidLoad {

    [superviewDidLoad];

    //测试数据,后期可以根据服务器返回的数据自己设置

    _arry=[NSMutableArrayarrayWithObjects:@"100",@"50",@"500",@"30",@"45",@"200",@"70",@"130",@"100",@"50",@"500",@"30",@"45",@"200",@"70",@"130",@"100",@"50",@"500",@"30",@"45",@"200",@"70",@"130",@"100",@"50",@"500",@"30",@"45",@"200",@"70",@"130",@"100",@"50",@"500",@"30",@"45",@"200",@"70",@"130",@"100",@"50",@"500",@"30",@"45",@"200",@"70",@"130",nil];

    self.view.backgroundColor=[UIColorwhiteColor];

    MYLayout*layout =[[MYLayoutalloc]init];

    layout.delegate=self;

    //layout.colCount=2;

    //

    //layout.delegate=self;

    UICollectionView*collection =[[UICollectionViewalloc]initWithFrame:self.view.boundscollectionViewLayout:layout];

    collection.delegate=self;

    collection.dataSource=self;

    collection.backgroundColor=[UIColorwhiteColor];

    [collectionregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:cellresure];

    [self.viewaddSubview:collection];

    // Do any additional setup after loading the view, typically from a nib.

    }

    ```

    ```Object

    -C-(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section

    {

    return_arry.count;

    }

    ```

    -(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath

    {

    UICollectionViewCell*cell =[collectionViewdequeueReusableCellWithReuseIdentifier:cellresureforIndexPath:indexPath];

    cell.backgroundColor=[UIColorredColor];

    UILabel*lal =(UILabel*)[cell.contentViewviewWithTag:100];

    if(lal==nil)

    {

    lal =[[UILabelalloc]init];

    lal.tag=100;

    [cell.contentViewaddSubview:lal];

    }

    lal.text=[NSStringstringWithFormat:@"%ld",(long)indexPath.row];

    [lalsizeToFit];

    returncell;

    }

    - (CGFloat)waterflowLayout:(MYLayout*)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath*)indexPath

    {

    CGFloatheight =[self.arry[indexPath.row]floatValue];

    returnheight;

    }

    -(NSInteger)waterFlowLayoutColumnCount:(MYLayout*)layout

    {

    return3;

    }

    -(CGFloat)waterFlowLayoutColumnSpacing:(MYLayout*)layout

    {

    return10;

    }

    -(CGFloat)waterFlowLayoutRowSpacing:(MYLayout*)layout

    {

    return10;

    }

    -(UIEdgeInsets)waterFlowLayoutEdgeInsets:(MYLayout*)layout

    {

    returnUIEdgeInsetsMake(20,20,20,20);

    }

    //

    - (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    @end

    自定义的类MYLayout.h

    #import

    @classMYLayout;

    @protocolMYLayoutDelegate

    //此方法必须实现,设置每个item的高度

    - (CGFloat)waterflowLayout:(MYLayout*)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath*)indexPath;

    @optional

    //设置collectionView有几列,如果不设置默认是3列

    -(NSInteger)waterFlowLayoutColumnCount:(MYLayout*)layout;

    //设置每个item的列间距,如果不设置默认10

    -(CGFloat)waterFlowLayoutColumnSpacing:(MYLayout*)layout;

    //设置每个item的行间距,如果不设置默认10

    -(CGFloat)waterFlowLayoutRowSpacing:(MYLayout*)layout;

    //设置item距离屏幕上下左右的距离,默认是上下左右都是10;

    -(UIEdgeInsets)waterFlowLayoutEdgeInsets:(MYLayout*)layout;

    @end

    @interfaceMYLayout :UICollectionViewFlowLayout

    @property(assign,nonatomic)iddelegate;

    @end

    自定义的类MYLayout.m

    #import"MYLayout.h"

    staticNSIntegerconstdefaultColumnCount =3;

    staticCGFloatconstdefaucolumspacesing =10;

    staticCGFloatconstdedaultRowsping =10;

    staticUIEdgeInsetsconstdefaultEdgeInsets = {10,10,10,10};

    @interfaceMYLayout();

    @property(nonatomic,strong) NSMutableArray * attrArray;

    @property(nonatomic,strong) NSMutableArray *maxYArray;

    -(NSInteger)columnCount;

    -(CGFloat)columSpacing;

    -(CGFloat)rowSpacing;

    -(UIEdgeInsets)edgeInsets;

    @end

    @implementationMYLayout

    -(NSMutableArray *)attrArray

    {

    if(_attrArray==nil)

    {

    _attrArray=[NSMutableArray array];

    }

    return_attrArray;

    }

    -(NSMutableArray *)maxYArray

    {

    if(_maxYArray ==nil)

    {

    _maxYArray =[NSMutableArray array];

    }

    return_maxYArray;

    }

    -(NSInteger)columnCount

    {

    if([self.delegate respondsToSelector:@selector(waterFlowLayoutColumnCount:)]) {

    return[self.delegate waterFlowLayoutColumnCount:self];

    }

    returndefaultColumnCount;

    }

    -(CGFloat)columSpacing

    {

    {

    if([self.delegate respondsToSelector:@selector(waterFlowLayoutColumnSpacing:)]) {

    return[self.delegate waterFlowLayoutColumnSpacing:self];

    }

    returndefaucolumspacesing;

    }

    }

    -(CGFloat)rowSpacing

    {

    {

    if([self.delegate respondsToSelector:@selector(waterFlowLayoutRowSpacing:)]) {

    return[self.delegate waterFlowLayoutRowSpacing:self];

    }

    returndedaultRowsping;

    }

    }

    -(UIEdgeInsets)edgeInsets

    {

    {

    if([self.delegate respondsToSelector:@selector(waterFlowLayoutEdgeInsets:)]) {

    return[self.delegate waterFlowLayoutEdgeInsets:self];

    }

    returndefaultEdgeInsets;

    }

    }

    -(void)prepareLayout

    {

    [superprepareLayout];

    [self.attrArray removeAllObjects];

    [self.maxYArray removeAllObjects];

    for(inti =0; i <[selfcolumnCount]; i ++)

    {

    [self.maxYArray addObject:@([selfedgeInsets].top)];

    }

    NSInteger itemCount =[self.collectionView numberOfItemsInSection:0];

    for(inti =0; i

    {

    NSIndexPath *indepath =[NSIndexPath indexPathForRow:i inSection:0];

    [self.attrArray addObject:[selflayoutAttributesForItemAtIndexPath:indepath ]];

    }

    }

    -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

    {

    returnself.attrArray;

    }

    -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

    {

    UICollectionViewLayoutAttributes*attabuest =[UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:indexPath];

    NSInteger__blockminHegihtColumn =0;

    NSInteger__blockminheight = [self.maxYArray[minHegihtColumn]floatValue];

    [self.maxYArrayenumerateObjectsUsingBlock:^(id_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {

    CGFloatcolumnHeight = [(NSNumber*)objfloatValue];

    if(minheight>columnHeight)

    {

    minheight =columnHeight;

    minHegihtColumn=idx;

    }

    }];

    UIEdgeInsetsedgeInsets =[selfedgeInsets];

    CGFloatwidth =(CGRectGetWidth(self.collectionView.frame)-edgeInsets.left-edgeInsets.right-[selfcolumSpacing]*([selfcolumnCount]-1))/[selfcolumnCount];

    CGFloatheight = [self.delegatewaterflowLayout:selfheightForWidth:widthatIndexPath:indexPath];

    CGFloatorginX =edgeInsets.left +minHegihtColumn *(width +[selfcolumSpacing]);

    CGFloat orginY = minheight;

    if(orginY != edgeInsets.top)

    {

    orginY +=[selfrowSpacing];

    }

    attabuest.frame =CGRectMake(orginX,orginY,width,height);

    self.maxYArray[minHegihtColumn]=@(CGRectGetMaxY(attabuest.frame));

    returnattabuest;

    }

    -(CGSize)collectionViewContentSize

    {

    NSInteger__blockmaxheight =0;

    [self.maxYArray enumerateObjectsUsingBlock:^(id_Nonnullobj, NSUInteger idx,BOOL*_Nonnullstop) {

    CGFloat columnHeight = [(NSNumber *)obj floatValue];

    if(maxheight

    {

    maxheight =columnHeight;

    }

    }];

    returnCGSizeMake(0, maxheight +[selfedgeInsets].bottom);

    }

    @end

    ```Object-C

    到此瀑布流已经实现

    相关文章

      网友评论

          本文标题:简单实现瀑布流

          本文链接:https://www.haomeiwen.com/subject/nuwwsttx.html