IrregularGridCollectionView处理不定宽度的标签cell

效果

源码

https://github.com/YouXianMing/UI-Component-Collection 中的 IrregularGridCollectionView

//
//  IrregularGridCollectionView.h
//  IrregularGridCollectionView
//
//  Created by YouXianMing on 16/8/30.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "IrregularGridCellDataAdapter.h"
#import "MaximumSpacingFlowLayout.h"
#import "CustomIrregularGridViewCell.h"
@class IrregularGridViewCellClassType;
@class IrregularGridCollectionView;@protocol IrregularGridCollectionViewDelegate <NSObject>@optional/***  IrregularGridCollectionView did selected event.**  @param collectionGridView CollectionGridView's object.*  @param cell               CustomCollectionGridViewCell type's cell.*  @param event              CustomCollectionGridViewCell's event.*/
- (void)irregularGridCollectionView:(IrregularGridCollectionView *)irregularGridCollectionView didSelectedCell:(CustomIrregularGridViewCell *)cell event:(id)event;@end@interface IrregularGridCollectionView : UIView/***  CollectionGridView's delegate.*/
@property (nonatomic, weak) id <IrregularGridCollectionViewDelegate> delegate;/***  CollectionView.*/
@property (nonatomic, strong, readonly) UICollectionView *collectionView;/***  Content edgeInsets, default is UIEdgeInsetsMake(5, 5, 5, 5).*/
@property (nonatomic) UIEdgeInsets contentEdgeInsets;/***  Horizontal item's gap, default is 5.f.*/
@property (nonatomic) CGFloat horizontalGap;/***  Vertical item's gap, default is 5.f.*/
@property (nonatomic) CGFloat verticalGap;/***  Item's height, default is 20.f.*/
@property (nonatomic) CGFloat gridHeight;/***  Register the cells.*/
@property (nonatomic, strong) NSArray <IrregularGridViewCellClassType *> *registerCells;/***  The cells data adapter.*/
@property (nonatomic, strong) NSMutableArray <IrregularGridCellDataAdapter *> *adapters;/***  To make the config effective.*/
- (void)makeTheConfigEffective;/***  Get the CollectionView's content size.*/
@property (nonatomic, readonly) CGSize contentSize;/***  Reset the view's size.*/
- (void)resetSize;#pragma mark - Constructor.+ (instancetype)irregularGridCollectionViewWithFrame:(CGRect)framedelegate:(id <IrregularGridCollectionViewDelegate>)delegateregisterCells:(NSArray <IrregularGridViewCellClassType *> *)registerCellscontentEdgeInsets:(UIEdgeInsets)edgeInsetsverticalGap:(CGFloat)verticalGaphorizontalGap:(CGFloat)horizontalGapgridHeight:(CGFloat)gridHeight;@end#pragma mark - CollectionGridViewCellClassType Class@interface IrregularGridViewCellClassType : NSObject@property (nonatomic)         Class      className;
@property (nonatomic, strong) NSString  *reuseIdentifier;@endNS_INLINE IrregularGridViewCellClassType *gridViewCellClassType(Class className, NSString  *reuseIdentifier) {IrregularGridViewCellClassType *type = [IrregularGridViewCellClassType new];type.className                        = className;type.reuseIdentifier                  = reuseIdentifier;return type;
}

//
//  IrregularGridCollectionView.m
//  IrregularGridCollectionView
//
//  Created by YouXianMing on 16/8/30.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "IrregularGridCollectionView.h"#pragma mark - IrregularGridCollectionView Class@interface IrregularGridCollectionView () <UICollectionViewDelegate, UICollectionViewDataSource, CustomIrregularGridViewCellDelegate>@property (nonatomic, strong) UICollectionView            *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout  *flowLayout;@end@implementation IrregularGridCollectionView#pragma mark - Init- (void)layoutSubviews {[super layoutSubviews];_collectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {self.contentEdgeInsets   = UIEdgeInsetsMake(5, 5, 5, 5);self.horizontalGap       = 5.f;self.verticalGap         = 5.f;self.gridHeight          = 20.f;// Init UICollectionViewFlowLayout.self.flowLayout = [[MaximumSpacingFlowLayout alloc] init];// Init UICollectionView.self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];self.collectionView.showsHorizontalScrollIndicator = NO;self.collectionView.showsVerticalScrollIndicator   = NO;self.collectionView.backgroundColor                = [UIColor clearColor];self.collectionView.delegate                       = self;self.collectionView.dataSource                     = self;[self addSubview:self.collectionView];}return self;
}- (void)makeTheConfigEffective {self.collectionView.contentInset        = self.contentEdgeInsets;self.flowLayout.minimumLineSpacing      = self.verticalGap;self.flowLayout.minimumInteritemSpacing = self.horizontalGap;
}#pragma mark - UICollectionView's delegate & data source.- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return _adapters.count;
}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {IrregularGridCellDataAdapter *adapter = _adapters[indexPath.row];adapter.indexPath                     = indexPath;CustomIrregularGridViewCell  *cell    = [collectionView dequeueReusableCellWithReuseIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];cell.delegate                         = self;cell.dataAdapter                      = adapter;cell.data                             = adapter.data;cell.indexPath                        = indexPath;cell.collectionView                   = collectionView;cell.collectionGridView = self;[cell loadContent];return cell;
}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {IrregularGridCellDataAdapter *adapter = _adapters[indexPath.row];return CGSizeMake(adapter.itemWidth, self.gridHeight);
}+ (instancetype)irregularGridCollectionViewWithFrame:(CGRect)framedelegate:(id <IrregularGridCollectionViewDelegate>)delegateregisterCells:(NSArray <IrregularGridViewCellClassType *> *)registerCellscontentEdgeInsets:(UIEdgeInsets)edgeInsetsverticalGap:(CGFloat)verticalGaphorizontalGap:(CGFloat)horizontalGapgridHeight:(CGFloat)gridHeight {IrregularGridCollectionView *irregularGridView = [[[self class] alloc] initWithFrame:frame];irregularGridView.delegate                     = delegate;irregularGridView.contentEdgeInsets            = edgeInsets;irregularGridView.verticalGap                  = verticalGap;irregularGridView.horizontalGap                = horizontalGap;irregularGridView.gridHeight                   = gridHeight;irregularGridView.registerCells                = registerCells;[irregularGridView makeTheConfigEffective];return irregularGridView;
}#pragma mark - CustomIrregularGridViewCellDelegate- (void)customIrregularGridViewCell:(CustomIrregularGridViewCell *)cell event:(id)event {if (self.delegate && [self.delegate respondsToSelector:@selector(irregularGridCollectionView:didSelectedCell:event:)]) {[self.delegate irregularGridCollectionView:self didSelectedCell:cell event:event];}
}#pragma mark - Setter & Getter- (void)setRegisterCells:(NSArray <IrregularGridViewCellClassType *> *)registerCells {_registerCells = registerCells;for (IrregularGridViewCellClassType *type in registerCells) {[self.collectionView registerClass:type.className forCellWithReuseIdentifier:type.reuseIdentifier];}
}- (CGSize)contentSize {CGSize size = [_flowLayout collectionViewContentSize];size.width  += self.contentEdgeInsets.left + self.contentEdgeInsets.right;size.height += self.contentEdgeInsets.top  + self.contentEdgeInsets.bottom;return size;
}- (void)resetSize {CGRect newFrame = self.frame;newFrame.size   = [self contentSize];self.frame      = newFrame;
}@end#pragma mark - IrregularGridViewCellClassType Class@implementation IrregularGridViewCellClassType@end

细节

转载于:https://www.cnblogs.com/YouXianMing/p/6038248.html

IrregularGridCollectionView处理不定宽度的标签cell相关推荐

  1. [html] 写出不定宽度的子级div,在相对于固定宽度的父级元素水平居中的布局

    [html] 写出不定宽度的子级div,在相对于固定宽度的父级元素水平居中的布局 <div class="father"> <div class="so ...

  2. android浮动文本,android 添加浮动标签在textView最尾端,自动换行

    1.文章背景 最近在做一个需求,效果图如下:先上张图 image 就是动态根据textView文本,追随一个标签在 最后面~ 其实代码也很简单,就是动态计算textView文本的宽度 和 标签的宽度, ...

  3. html 控制文字的标签,html里面有一个控制文字滚动的标签marquee,比较有用。

    简介这篇文章主要介绍了html里面有一个控制文字滚动的标签marquee,比较有用.以及相关的经验技巧,文章约3183字,浏览量161,点赞数4,值得参考! 本节笔者讲述HTML代码中比较特殊的标签, ...

  4. 背景图宽度自适应及背景图合并的CSS思想

    关于宽度自适应,已经是前段开发人员必备的css技能之一,而背景图的合并则属于更高级别的要求. 我们为什么要宽度自适应,原因大体有以下几点: 第一:很多情况下有这样的需求,比如做B/S前端,90%以上要 ...

  5. 常用 html 标签

    标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD). 标签:a 说明:标明超链接的起始或目的位置. 标签:acronym 说明:标明缩写词. 标签:address 说明: ...

  6. CSS基础-标签显示模式(display)【学习笔记】

    标签的三种显示模式 三种显示模式的特点以及区别 三种显示模式的相互转化 什么是标签显示模式 什么是标签的显示模式? 标签以什么方式进行显示,比如div 自己占一行, 比如span 一行可以放很多个 作 ...

  7. html表格宽度拖拽,原生js实现 拖拽改变 table表格列宽

    table 员工编号 试用时间 转正时间 性别 姓名拼音 生日时间 民族 身高 vh20180421 2018-3-13 2018-6-13 1 LDH 1988-3-13 汉族 178 vh2018 ...

  8. python中一般使用几个空格表示缩进_为什么Python pep-8强烈建议使用标签上的空格来缩进?...

    为什么Python pep-8强烈建议使用标签上的空格来缩进? 我在Stack Overflow和PEP 8上看到,建议仅在Python程序中使用空格进行缩进. 我能理解一致压痕的必要性,我感到痛苦. ...

  9. Excel随着表格内容自动调整高度或宽度

    2019独角兽企业重金招聘Python工程师标准>>> Excel随着表格内容自动调整高度或宽度 在使用excel的时候,有时我们需要根据输入内容的多少调整表格的高度和宽度,如何让E ...

最新文章

  1. java直接打开word_Java
  2. 维护窗口和停机时间 可用率99.99%
  3. 用django将数据从数据库提出并分页展示
  4. python3 错误string indices must be integers 的解决方法
  5. android原生组件,XUI: 一个简洁而优雅的Android原生UI框架,解放你的双手!
  6. Python正则表达式之元字符详解(1)
  7. 三年磨一剑,五次被拒稿,交大博士坚持稿件申诉,终发表学科顶刊
  8. php 处理 mysql to json, 前台js处理
  9. 基于特征的对抗迁移学习论文_[论文笔记] 对抗样本不是bugs,而是特征
  10. 带你自学Python系列(十二):Python函数的用法(二)
  11. 消息推送服务器令牌,小程序-消息推送配置Token令牌错误校验失败如何解决
  12. 恶意软件--》木马、病毒、蠕虫
  13. 错误解决:release' is unavailable: not available in automatic reference counting mode
  14. el表达式字符串与变量拼接
  15. 哪些情况会造成小程序违规或下架
  16. 荨麻疹自我治理指南(民间版)
  17. EC-PCA: 利润中心会计流程设计和方案要点
  18. VB.net实现通讯录
  19. 节日活动发布,选对H5让你在老板面前大放异彩!
  20. SparkSQL函数定义——UDF函数,窗口函数

热门文章

  1. Effective C++ 读书笔记(八)
  2. java中判断字段真实长度(中文2个字符,英文1个字符)的方法
  3. jquery数组(操作数组元素)
  4. VisualC++2010系列课程
  5. 使用redis做为MySQL的缓存
  6. javascript --- 在linux上部署项目
  7. Java Web 请求转发与请求重定向
  8. 企业选择 多云管理平台 六大注意事项
  9. /home文件夹重新划分独立分区
  10. 海信FW3010-5000H千兆防火墙