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)frame
delegate:(id <IrregularGridCollectionViewDelegate>)delegate
registerCells:(NSArray <IrregularGridViewCellClassType *> *)registerCells
contentEdgeInsets:(UIEdgeInsets)edgeInsets
verticalGap:(CGFloat)verticalGap
horizontalGap:(CGFloat)horizontalGap
gridHeight:(CGFloat)gridHeight; @end #pragma mark - CollectionGridViewCellClassType Class @interface IrregularGridViewCellClassType : NSObject @property (nonatomic) Class className;
@property (nonatomic, strong) NSString *reuseIdentifier; @end NS_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(, , self.frame.size.width, self.frame.size.height);
} - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.contentEdgeInsets = UIEdgeInsetsMake(, , , );
self.horizontalGap = .f;
self.verticalGap = .f;
self.gridHeight = .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)frame
delegate:(id <IrregularGridCollectionViewDelegate>)delegate
registerCells:(NSArray <IrregularGridViewCellClassType *> *)registerCells
contentEdgeInsets:(UIEdgeInsets)edgeInsets
verticalGap:(CGFloat)verticalGap
horizontalGap:(CGFloat)horizontalGap
gridHeight:(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

细节

IrregularGridCollectionView处理不定宽度的标签cell的更多相关文章

  1. iOS tableView移除某一行的分割线 让分割线宽度为整个cell的宽度

    移除tableViewCell中某一行的分割线 有2种方法 1. 移除系统的分割线,自己定义每行的分割线 self.tableView.separatorStyle = UITableViewCell ...

  2. pc端常见布局---水平居中布局 单元素不定宽度

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. html+css--水平居中总结(不定宽块状元素方法)(一)

    来源:http://www.imooc.com/code/6363 在实际工作中我们会遇到需要为“不定宽度的块状元素”设置居中,比如网页上的分页导航,因为分页的数量是不确定的,所以我们不能通过设置宽度 ...

  4. CSS 技巧一则 -- 不定宽溢出文本适配滚动

    在日常布局当中,肯定经常会遇到文本内容超过容器的情况.非常常见的一种解决方案是超出省略. 但是,有的时候,由于场景的限制,可能会出现在一些无法使用超出打点省略的方法的场景,譬如在导航栏中: 这种情况下 ...

  5. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  6. JS实现自适应宽度的Tag切换

    效果体验:http://hovertree.com/texiao/js/3.htm 该效果使用纯JavaScript代码,实现TAB页切换效果,TAB标签根据内容自适应宽度,点击TAB标签切换内容页. ...

  7. 自定义Cell的方法

    Cell属于UITableView中的组件,有多种定义方式,有系统自带的方法,有自定义的方法. 可以使用系统的方法setSeparatorColor(设置分割线颜色) 设置setSeparatorSt ...

  8. 仿网易漂亮的TAB选项卡(标签)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. HTML标签大全

    HTML标签解释大全 一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD). 标签:a 说明:标明超链接的起始或目的位置. 标签:acronym 说明:标 ...

随机推荐

  1. 深入理解C语言的函数调用过程

    本文主要从进程栈空间的层面复习一下C语言中函数调用的具体过程,以加深对一些基础知识的理解.     先看一个最简单的程序: 点击(此处)折叠或打开 /*test.c*/ #include stdio. ...

  2. 0801 am使用tp框架对数据库增删改查

    增添数据,3种方法 function Text3() { $m=D("info"); //1.使用数组 $attr = array( "code"=>&q ...

  3. struts2 表单验证

    http://www.blogjava.net/javagrass/archive/2011/11/21/364400.html

  4. Browser设置UA值

    SWE Browser中的OptionMenu是Controller通过onKeyDown监听KEYCODE_MENU来显示的 public boolean onKeyDown(int keyCode ...

  5. 实现毛玻璃模糊效果/DRNRealTimeBlur

    四种方法:1.美工出图  2.coreImage框架,高斯效果 3.ToolBar,覆盖在view上边 //1.添加图片 self.imageView.image = [UIImage imageNa ...

  6. Reapter控件的特殊使用:使用EVAL调取asp:Repeater里面绑定的值来进行判断 根据从数据库获取的数据进行判断 ,进而显示成想要的内容

    1.这个判断的过程你可以写在后台,如先在后台写一个public类型的方法:public bool CheckAduit(string code){ //根据你传入的code来判断,并返回true或者f ...

  7. 用Fragment制作的Tab页面产生的UI重叠问题

    本文出处:http://blog.csdn.net/twilight041132/article/details/43812745 在用Fragment做Tab页面,发现有时候进入应用会同时显示多个T ...

  8. Exception in thread “main” com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String

    String json="A valid json"; Job job = new Gson().fromJson(json, Job.class); Exception in t ...

  9. hdu 5101 n集合选2个不同集合数使和大于k

    http://acm.hdu.edu.cn/showproblem.php?pid=5101 给n个集合,选择两个来自不同集合的数,加和大于k,问有多少种选择方案. 答案=从所有数中选择的两个加和大于 ...

  10. Zabbix_server.conf 的性能调优

    Zabbix安装完成后,模板里面有一个Template App Zabbix Server,添加到zabbix服务器里. 过个一两天,查看以下的图表(在Graphs里面). Zabbix cache ...