美文网首页
scrollView实现瀑布流

scrollView实现瀑布流

作者: krokil | 来源:发表于2016-03-06 22:03 被阅读0次

    scrollView完成瀑布流:

    1.根据tableView设计一个继承于scrollView的waterFlawLayoutView:

    设置数据源dataSource和代理delegate方法:

     1>询问数据源要数据:

    //cell的对应的数据一共有多少个

    -(NSInteger)numberOfCellInWaterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView;

    //对应下标的cell的数据

    -(LYWaterFlawVIewCell *)waterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView cellAtIndex:(NSUInteger )index;

    //列数

    -(NSUInteger)numberOfColumnInWaterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView;

     2>代理方法

    注意:@protocol LYWaterFlawLayoutViewDelegate<UIScrollView>//记得要成为scrollview的代理

    -(void)waterFlawView:(LYWaterFlawLayoutVIew *)waterFlawView didSelectedAtIndex:(NSInteger )index;

    //每列的高度

    - (CGFloat)waterflowView:(LYWaterFlawLayoutVIew  *)waterflowView heightAtIndex:(NSUInteger)index;

    //间距

    - (CGFloat)waterflowView:(LYWaterFlawLayoutVIew  *)waterflowView intervalForType:(LYWaterFlawLayoutVIewIntervalType)type;

    //学习苹果的tableView高度是在delelgagte的,但为什么高度是放在delegate而不是在数据源方法中,why?

    //接口

    //刷新数据

    -(void)reloadData;

    //用标识符从缓冲池中换取

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;

    2.LYWaterFlawViewCell

    //提供标识符

    @property (nonatomic, copy) NSString *identifier;//标识符

    3LYWaterFlawView

    //保存每个cellde 大小位置

    @property(nonatomic,strong)NSMutableArray *cellFrames;

    //正在显示的cell

    @property(nonatomic,strong)NSMutableDictionary *showingInScreenCell;

    //cell的缓存池

    @property (nonatomic, strong) NSMutableSet *reusableCells;

    @implementation

    //刷新重新计算所有cell的frame

    -(void)reloadData {

    /问数据源拿到总cell的数目

    NSUInteger numberOfCells  = [self.dataSource numberOfCellInWaterFlawView:self];

    //问数据源拿到总的列数

    NSUInteger numberOfColumns = [self numberOfColumns];

    //所有的间距

    CGFloat topInterval        = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeTop];  //上

    CGFloat leftInterval      = [self intervalOfType:LYWaterFlawLayoutVIewInterValTypeLeft];  //左

    CGFloat bottomInterval    = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeBottom];//下

    CGFloat rightInterval      = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeRight]; //右

    CGFloat columnInterval    = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeColumn];//列

    CGFloat rowInterval        = [self intervalOfType:LYWaterFlawLayoutVIewIntervalTypeRow];  //行

    //cell的宽度

    CGFloat cellWidth          = (self.width - (numberOfColumns -1)*columnInterval - leftInterval -rightInterval) / numberOfColumns;

    //C的数组用于保存每列的当前的高度

    CGFloat maxY[numberOfColumns];

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

    maxY[i] = 0.0;

    }

    }

    for (int i = 0 ; i < numberOfCells; i++)

     {        NSUInteger minCellColumn = 0;//记录最小Y的那列        CGFloat minYOfColumns = maxY[minCellColumn];//设最小那列的y值                for (int j = 1; jcontentY) {

    contentY = maxY[i];

    }

    相关文章

      网友评论

          本文标题:scrollView实现瀑布流

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