用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流

实现的瀑布流效果图:

用CHTCollectionViewWaterfallLayout写瀑布流用CHTCollectionViewWaterfallLayout写瀑布流

用CHTCollectionViewWaterfallLayout写瀑布流用CHTCollectionViewWaterfallLayout写瀑布流

源码:

WaterfallCell.h 与 WaterfallCell.m

//
//  WaterfallCell.h
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface WaterfallCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *showImageView;

@end
//
//  WaterfallCell.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "WaterfallCell.h"

@implementation WaterfallCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Scale the imageview to fit inside the contentView with the image centered:
        CGRect imageViewFrame = CGRectMake(0.f, 0.f,
                                           CGRectGetMaxX(self.contentView.bounds),
                                           CGRectGetMaxY(self.contentView.bounds));
        _showImageView                  = [UIImageView new];
        _showImageView.contentMode      = UIViewContentModeScaleAspectFill;
        _showImageView.frame            = imageViewFrame;
        _showImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _showImageView.clipsToBounds    = YES;
        [self addSubview:_showImageView];
        self.layer.borderWidth = 1.f;
    }
    return self;
}

@end


HeaderView.h 与 HeaderView.m

//
//  HeaderView.h
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HeaderView : UICollectionReusableView

@end
//
//  HeaderView.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "HeaderView.h"

@implementation HeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderWidth = 1.f;
    }
    return self;
}

@end


FooterView.h 与 FooterView.m

//
//  FooterView.h
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FooterView : UICollectionReusableView

@end
//
//  FooterView.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "FooterView.h"

@implementation FooterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderWidth = 1.f;
    }
    return self;
}

@end


使用时候的代码:

//
//  RootViewController.m
//  UICollectionView
//
//  Created by YouXianMing on 14-9-17.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "RootViewController.h"
#import "CHTCollectionViewWaterfallLayout.h"

#import "WaterfallCell.h"
#import "HeaderView.h"
#import "FooterView.h"

#define CELL_IDENTIFIER   @"WaterfallCell"
#define HEADER_IDENTIFIER @"WaterfallHeader"
#define FOOTER_IDENTIFIER @"WaterfallFooter"

@interface RootViewController ()<UICollectionViewDataSource, CHTCollectionViewDelegateWaterfallLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray   *dataSource;
@property (nonatomic, strong) NSMutableArray   *dataSourceSize;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 数据源
    _dataSource = [NSMutableArray new];
    for (int i = 0; i <= 16; i++)
    {
        [_dataSource addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d", i]]];
    }

    // 初始化布局
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    
    // 设置布局
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerHeight = 100;           // headerView高度
    layout.footerHeight = 100;           // footerView高度
    layout.columnCount  = 4;             // 几列显示
    layout.minimumColumnSpacing    = 5;  // cell之间的水平间距
    layout.minimumInteritemSpacing = 5;  // cell之间的垂直间距
    
    // 初始化collectionView
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
                                         collectionViewLayout:layout];
    _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _collectionView.dataSource       = self;
    _collectionView.delegate         = self;
    _collectionView.backgroundColor  = [UIColor whiteColor];
    
    // 注册cell以及HeaderView,FooterView
    [_collectionView registerClass:[WaterfallCell class]
        forCellWithReuseIdentifier:CELL_IDENTIFIER];
    [_collectionView registerClass:[HeaderView class]
        forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
               withReuseIdentifier:HEADER_IDENTIFIER];
    [_collectionView registerClass:[FooterView class]
        forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
               withReuseIdentifier:FOOTER_IDENTIFIER];
    
    // 添加到视图当中
    [self.view addSubview:_collectionView];
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"您点击了 %@", _dataSource[indexPath.row]);
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    // 数据源
    return [_dataSource count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    // 1个区
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 注册collectionViewCell
    WaterfallCell *cell = \
        (WaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER
                                                                   forIndexPath:indexPath];
    
    UIImage *image           = _dataSource[indexPath.row];
    cell.showImageView.image = image;
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    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];
    }
    
    return reusableView;
}

#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 用以返回尺寸
    UIImage *image = _dataSource[indexPath.row];
    return image.size;
}

@end

这个代码再怎么简单也会很复杂-_-!!

以下是使用的细节需要注意的地方:

设置的对应关系-

用CHTCollectionViewWaterfallLayout写瀑布流

cell中的图片可不是随便设置的-

用CHTCollectionViewWaterfallLayout写瀑布流

要注意返回每个cell的size值-

用CHTCollectionViewWaterfallLayout写瀑布流

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109066.html原文链接:https://javaforall.net

(0)
上一篇 2021年9月14日 上午9:00
下一篇 2021年9月14日 上午10:00


相关推荐

  • server2003怎么进入安全模式_windows2003server

    server2003怎么进入安全模式_windows2003serverWINDOWS SERVER 2003 上出现 超过响应缓冲区限制 错误,如何解决?Response 对象 错误 ASP 0251 : 80004005超过响应缓冲区限制/py11g/list.asp,行 0此 ASP 页的执行造成响应缓冲区超过其配置限制。我要RESPONSE.WRITE 的数据量的确非常大,但是必须回写的,如何更改设置或者程序 ———————–

    2022年8月31日
    7
  • libevent 定时器

    libevent 定时器#include#include#include#include#include#include#include#includestructeventev;structtimevaltv;voidtime_cb(intfd,shortevent,void*argc){printf(“timerwakeup\n”);

    2025年6月6日
    9
  • pycharm代码规范工具flake8

    pycharm代码规范工具flake8文章目录代码规范一、flake8是什么?二、使用步骤1.引入库2.读入数据总结代码规范随着计算机语言的发展,代码规范是我们写代码时更应该注意的东西,在我们使用pycharm进行代码编写时,我们可以借用pycharm工具flake8进行代码规范检查,从而使代码规范符合标准要求。代码规范可以减少bug,有助于代码审查,降低维护成本,更重要的是在大型项目中,大家使用一套共同的代码标准,这更能够提升团队间的合作效率。一、flake8是什么?Flake8是一款辅助检测Python代码是否规范的工具

    2025年9月3日
    6
  • Excel数据快速导入mysql的几个办法

    Excel数据快速导入mysql的几个办法有时候需要批量插入一批数据到数据库 有很多种办法 这里我用到过三种办法 1 通过 Excel 直接生成 insert 语句 CONCATENATE insertintoai pingfen fengcai id order n department subject n teacher name values A1 B1 amp

    2026年3月20日
    2
  • c++ stl源码剖析_stl源码 qt源码

    c++ stl源码剖析_stl源码 qt源码C++stl库手写前言序列式关联式容器适配器ListVector函数dequestringstackqueuebitset关联式容器setmultisetmultiset算法库仿函数前言stl版本abcd,四个版本,接口肯定是一样的代码复用性强,效率高,通用性高,vectordeque他有六个组件,空间配置器,容器,迭代器,算法,仿函数,容器适配器容器和算法中间,靠迭代器连接算法为了通用性,有辅助的东西,让算法通用,也就是使用仿函数仿函数就是一个对象容器通过适配器,可以相

    2022年10月15日
    3
  • 单链表的头插法与尾插法详解及实现(C语言)[通俗易懂]

    单链表的头插法与尾插法详解及实现(C语言)[通俗易懂]单链表的建立有头插法和尾插法首先是定义一个结构体#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#defineElemTypeinttypedefstructLNode{ ElemTypeData;//数据域 structLNode*Ne…

    2022年5月19日
    88

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号