瀑布流教程2

接教程1,我们接着进行第三步。

第三步,就是实现瀑布流的整体了,瀑布流的整体跟TableView很相似。下面看代码

//.h文件

#import <UIKit/UIKit.h>

#import "WaterFlowViewCell.h"


@protocol WaterFlowViewDataSource;

@protocol WaterFlowViewDelegate;

@class WaterFlowView;



@interface WaterFlowView :UIScrollView<UITableViewDelegate,UITableViewDataSource>{


    int _columnCount;   //列数

    int _cellsTotal;    //总的数据项

    float _cellWidth;   //每列的宽度


    //UIActivityIndicatorView *activityViewLoad;   //活动指示器,进度滚轮指示器

    NSMutableArray *tableviews;

    

    

    id<WaterFlowViewDelegate> _waterFlowViewDelegate;

    id<WaterFlowViewDataSource> _waterFlowViewDatasource;

}


@property (nonatomic,assign)int columnCount; 

@property (nonatomic,assign)int cellsTotal; 

@property (nonatomic,assign)float cellWidth;

@property (nonatomic,assign)id<WaterFlowViewDelegate> waterFlowViewDelegate;

@property (nonatomic,assign)id<WaterFlowViewDataSource> waterFlowViewDatasource;


- (void)reloadData;  

- (NSInteger)waterFlowView:(WaterFlowView *)waterFlowView numberOfRowsInColumn:(NSInteger)colunm;

-(void)relayoutDisplaySubviews;


@end


/*

 代理方法

 */

@protocol WaterFlowViewDelegate <NSObject>


@optional


/*

 返回每个cell的高度

*/

- (CGFloat)waterFlowView:(WaterFlowView *)waterFlowView heightForRowAtIndexPath:(IndexPath *)indexPath;



/*

    跟据参数indexPath的行列属性,定位哪行哪列被选中

 */

- (void)waterFlowView:(WaterFlowView *)waterFlowView didSelectRowAtIndexPath:(IndexPath *)indexPath;



@end




/*

    数据源

*/

@protocol WaterFlowViewDataSource<NSObject>


@optional


/*

    配置每个cell的子视图,添加返回的视图到cell

 */


- (UIView *)waterFlowView:(WaterFlowView *)waterFlowView cellForRowAtIndexPath:(IndexPath *)indexPath;


/*

    cell上的子视图传出,方便自定义子视图的样式,实现个性化定制

 */

-(void)waterFlowView:(WaterFlowView *)waterFlowView  relayoutCellSubview:(UIView *)view withIndexPath:(IndexPath *)indexPath;



/*

    返回瀑布效果的列数

 */

- (NSInteger)numberOfColumsInWaterFlowView:(WaterFlowView *)waterFlowView;


/*

    返回cell的总数,每个cell的行号通过总数和列号来确定

 */

- (NSInteger)numberOfAllWaterFlowView:(WaterFlowView *)waterFlowView;


@end


//.m文件

#import "WaterFlowView.h"


#define TABLEVIEWTAG 1000

#define CELLSUBVIEWTAG 10000

@implementation WaterFlowView

@synthesize columnCount = _columnCount, cellsTotal = _cellsTotal,cellWidth =_cellWidth;

@synthesize waterFlowViewDelegate = _waterFlowViewDelegate,waterFlowViewDatasource = _waterFlowViewDatasource;


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {        

        self.contentSize =CGSizeMake(CGRectGetWidth(self.frame),CGRectGetHeight(self.frame)+1);

    }

    return self;

}


-(void)layoutSubviews{

    

    [selfrelayoutDisplaySubviews];

    

    

    for (UITableView *tableviewintableviews) {

        

        [tableview setFrame:CGRectMake(tableview.frame.origin.x,self.contentOffset.y,CGRectGetWidth(tableview.frame),CGRectGetHeight(tableview.frame))];

        [tableview setContentOffset:self.contentOffsetanimated:NO];

    }

    

    [superlayoutSubviews];

}



-(void)relayoutDisplaySubviews{


    self.columnCount=3;

    self.cellsTotal = [self.waterFlowViewDatasourcenumberOfAllWaterFlowView:self];

    

    if (_cellsTotal ==0 ||_columnCount ==0) {

        

        return;

    }

    

    self.cellWidth =CGRectGetWidth(self.frame)/_columnCount;//每列的宽度

    

    if (!tableviews) {

        

        tableviews = [[NSMutableArrayalloc]initWithCapacity:_columnCount];

        for (int i =0; i <_columnCount; i++) {

            

            UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(_cellWidth*i,0,_cellWidth,CGRectGetHeight(self.frame))style:UITableViewStylePlain];

            tableView.delegate = self;

            tableView.dataSource = self;

            tableView.tag = i + TABLEVIEWTAG; //保存列号

            

            tableView.showsVerticalScrollIndicator =NO;

            tableView.scrollEnabled = NO;

            tableView.separatorStyle =UITableViewCellSeparatorStyleNone;

            tableView.backgroundColor = [UIColorclearColor];

            [self addSubview:tableView];

            

            [tableviews addObject:tableView];


        }

    }

}


-(void)setWaterFlowViewDatasource:(id<WaterFlowViewDataSource>)waterFlowViewDatasource{


    _waterFlowViewDatasource = waterFlowViewDatasource;

}


-(void)setWaterFlowViewDelegate:(id<WaterFlowViewDelegate>)waterFlowViewDelegate{


    _waterFlowViewDelegate = waterFlowViewDelegate;

  

}


- (void)reloadData{

    [selfrelayoutDisplaySubviews];

    

    float contenSizeHeight;

    

    for (UITableView *tabelviewintableviews) {

        

         [tabelview reloadData];

        if (contenSizeHeight < tabelview.contentSize.height) {

            

            contenSizeHeight = tabelview.contentSize.height;

        }

    }

    

    contenSizeHeight = contenSizeHeight < CGRectGetHeight(self.frame)?CGRectGetHeight(self.frame)+1:contenSizeHeight;

    self.contentSize =CGSizeMake(self.contentSize.width, contenSizeHeight);

}



- (NSInteger)waterFlowView:(WaterFlowView *)waterFlowView numberOfRowsInColumn:(NSInteger)colunm{

    

    if (waterFlowView.cellsTotal -(colunm +1) <0) {

        

        return 0;

    }else{

        return (waterFlowView.cellsTotal -(colunm +1))/waterFlowView.columnCount+1;

    }

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return [selfwaterFlowView:selfnumberOfRowsInColumn:tableView.tag -TABLEVIEWTAG];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

     

    IndexPath *_indextPath = [IndexPathinitWithRow:indexPath.row withColumn:tableView.tag - TABLEVIEWTAG];

    CGFloat cellHeight = [self.waterFlowViewDelegatewaterFlowView:selfheightForRowAtIndexPath:_indextPath];

    return cellHeight;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    UITableViewCell *cell =nil;

    NSString *cellIndetify = [NSStringstringWithFormat:@"cell%d",tableView.tag -TABLEVIEWTAG];

    

    cell = [tableView dequeueReusableCellWithIdentifier:cellIndetify];

    if (cell == nil) {

    

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndetify];

        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        

        IndexPath *_indextPath = [IndexPathinitWithRow:indexPath.row withColumn:tableView.tag - TABLEVIEWTAG];

        UIView *cellSub =  [self.waterFlowViewDatasourcewaterFlowView:selfcellForRowAtIndexPath:_indextPath];

        [cell.contentView addSubview:cellSub];

        cellSub.tag = CELLSUBVIEWTAG;

    }

    

    IndexPath *_indextPath = [IndexPathinitWithRow:indexPath.row withColumn:tableView.tag - TABLEVIEWTAG];

    

    CGFloat cellHeight = [self.waterFlowViewDelegatewaterFlowView:selfheightForRowAtIndexPath:_indextPath];

    CGRect cellRect = CGRectMake(0,0,_cellWidth, cellHeight);

    [[cell viewWithTag:CELLSUBVIEWTAG]setFrame:cellRect];

    

    [self.waterFlowViewDatasourcewaterFlowView:selfrelayoutCellSubview:[cellviewWithTag:CELLSUBVIEWTAG]withIndexPath:_indextPath];


    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    IndexPath *_indextPath = [IndexPathinitWithRow:indexPath.row withColumn:tableView.tag - TABLEVIEWTAG];

    [self.waterFlowViewDelegatewaterFlowView:selfdidSelectRowAtIndexPath:_indextPath];

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值