深入了解CHTCollectionViewDelegateWaterfallLayout

一.这个是一个git上非常出名的写瀑布流的collectView第3方库 包含了OC和SWIFT版本 这里解析的主要是oc Demo方法
https://github.com/chiahsien/CHTCollectionViewWaterfallLayout

这篇文章主要介绍这个库的使用 如果对UICollectView非常感兴趣的的小伙伴 推荐你们看看
http://www.jianshu.com/p/df431a702135

开始

初始化UICollectionView


- (UICollectionView *)collectionView {
  if (!_collectionView) {
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    //区内边距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //区头内边距
    layout.headerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //区尾内边距
    layout.footerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //cell个数
    layout.columnCount = 30;
    //区头高度
    layout.headerHeight = 40;
    //区尾高度
    layout.footerHeight = 10;
    //列间距
    layout.minimumColumnSpacing = 10;
    //行间距
    layout.minimumInteritemSpacing = 10;
    //瀑布流方向
    layout.itemRenderDirection = CHTCollectionViewWaterfallLayoutItemRenderDirectionRightToLeft;
    //cell最小高度
    layout.minimumContentHeight = 50;
    //返回某个区的cell的宽度
    [layout itemWidthInSectionAtIndex:1];
      
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[CHTCollectionViewWaterfallCell class]
        forCellWithReuseIdentifier:CELL_IDENTIFIER];
    [_collectionView registerClass:[CHTCollectionViewWaterfallHeader class]
        forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
               withReuseIdentifier:HEADER_IDENTIFIER];
    [_collectionView registerClass:[CHTCollectionViewWaterfallFooter class]
        forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
               withReuseIdentifier:FOOTER_IDENTIFIER];
  }
  return _collectionView;
}

cell的定义

  CHTCollectionViewWaterfallCell *cell =
  (CHTCollectionViewWaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER
                                                                              forIndexPath:indexPath];

区头区尾的初始化

if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]) {
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                              withReuseIdentifier:HEADER_IDENTIFIER
                                                                     forIndexPath:indexPath];
        }
        else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter]) {
            reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                              withReuseIdentifier:FOOTER_IDENTIFIER
                                                                     forIndexPath:indexPath];
        }

这些方法 除去第一个定义cell大小 初始化上都可以设置但是这里可以根据你的section 分别作定义

//定义collectView 每个indexPath 的cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  return [self.cellSizes[indexPath.item %8] CGSizeValue];
}

//定义列数
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout columnCountForSection:(NSInteger)section{
    return 5;
}

//定义每个区的区头高度
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 200;
    }
    return 40;
}
//定义每个区的区尾高度
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForFooterInSection:(NSInteger)section{
    return 0;
}

//定义整个区的inset内边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return (UIEdgeInsets){30,10,20,20};
}

//定义区头的inset内边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForHeaderInSection:(NSInteger)section{
    return (UIEdgeInsets){30,10,20,20};
}
//定义区尾的inset内边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForFooterInSection:(NSInteger)section{
    return (UIEdgeInsets){30,10,20,20};
}
//定义整个区的每行 内部cell的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
//定义整个区的每列的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumColumnSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,615评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 6,233评论 4 8
  • 步骤:发微博01-导航栏内容 -> 发微博02-自定义TextView -> 发微博03-完善TextView和...
    dibadalu阅读 3,193评论 1 3
  • 回这一趟老家,心里多了两个疙瘩。第一是堂姐现在谈了一个有妇之夫,在她的语言中感觉,她不打算跟他有太长远的计划,这让...
    安九阅读 3,576评论 2 4