用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写瀑布流

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • MFC学习——下检测计算机是否联网

    MFC学习——下检测计算机是否联网一个最简单的类方法:Bool IsNetworkAlive( __out LPDWORD lpdwFlags);返回TRUE表示联网,FALSE表示未连接到网络。使用时注意在头文件中加入如下代码:#include <Sensapi.h>#pragma comment(lib, “Sensapi.lib”)函数使用举例: DWORD ws;…

  • 浅谈RHEL7和RHEL6的主要变化

    浅谈RHEL7和RHEL6的主要变化

  • 强化学习——Q学习算法「建议收藏」

    强化学习——Q学习算法「建议收藏」强化学习的一些相关概念智能体(Agent):智能体对环境进行观察,决策出行动,获得一个从环境返回的奖励决策(Decision):意识层面的行动(Action,a):物质层面的环境(Environment):与智能体交互的对象状态(State,s):是历史信息的函数,包含所有已有的信息。奖励(Reward,R):是智能体采取行动后环境的一个反馈策略(Policy):是状态到动作的函数价值函数(Valuefunction):是评价状态的一个指标模型(Model):是个体对环境的建模

  • 从零开始学习UCOSII操作系统1–UCOSII的基础知识

    从零开始学习UCOSII操作系统1–UCOSII的基础知识从零开始学习UCOSII操作系统1–UCOSII的基础知识前言:首先比较主流的操作系统有UCOSII、FREERTOS、LINUX等,UCOSII的资料相对比其余的两个操作系统的资料是多很多的。更重要的原因是自己本身还没有能力深入的研究Linux操作系统。本次学习UCOSII主要是学习内核的设计原理。此次专栏涉及到的API的使用是非常小的,仅仅作为本人学习的记录。后期也会对比UCOSIII说出实…

  • pycharm 激活码3月最新在线激活

    pycharm 激活码3月最新在线激活,https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • CSS常用颜色代码对照表

    CSS常用颜色代码对照表FFFFFF#DDDDDD#AAAAAA#888888#666666#444444#000000#FFB7DD#FF88C2#FF44AA #FF0088 #C10066 #A20055 #8C0044 #FFCCCC#FF8888#FF3333 

发表回复

您的电子邮箱地址不会被公开。

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