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. .net mvc 微信支付

    一.微信第三方登录 通过微信打开链接:http://www.hzm.com/Entry/Login 微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server ...

  2. C# web winform 路径

    string path = "";                if (System.Environment.CurrentDirectory == AppDomain.Curr ...

  3. DP专题——括号序列

    毕竟是个渣,写完一遍之后又按LRJ的写了一遍,再写了一遍递归版,最终加上输出解部分 括号序列 定义如下规则序列(字符串): 空序列是规则序列: 如果S是规则序列,那么(S)和[S]也是规则序列: 如果 ...

  4. 再见Unity3d的死循环

    前两天看见http://www.manew.com/thread-89909-1-1.html这篇译文之前几个小时刚好解决了一个莫名的死循环问题,然后忍不住要把另外一种方法告诉蛮友们.这个方法不需要知 ...

  5. Xcode升级更新后,恢复cocoapods以及插件的方法

    今天将手机系统更新到iOS9.3了,在Xcode7.1上做真机调试,提示找不到适合的SDK,才知道必须要升级Xcode才行,于是升级Xcode到7.3. 升级之后遇到很多麻烦,cocoapods没有了 ...

  6. 理解POCO

    理解POCO(Plain Old CLR Object)先要理解POJO. 1.什么是POJO? POJO的名称有多种,pure old java object .plain ordinary jav ...

  7. Log4j 配置数据库连接池(将日志信息保存到数据库)

    org.apache.log4j.jdbc.JDBCAppender 是利用传统的 JDBC 连接方法,这种方式连接数据库效率低下,为了解决这个问题,现在自定义一个 Log4j 的 Appender, ...

  8. selenium通过WebDriverWait实现ajax测试,实现等页面元素加载完成

    WebDriverWait(driver, 10)10秒内每隔500毫秒扫描1次页面变化,当出现指定的元素后结束. http://fox1984.iteye.com/blog/1225265new W ...

  9. 套题 codeforces 359

    A题:Free Ice Cream 注意要使用LL,避免爆int #include <bits/stdc++.h> #define scan(x,y) scanf("%d%d&q ...

  10. zabbix_agentd安装脚本共享

    #!/bin/bash wget -P /expect_zabbix/ http://10.107.2.44/zabbix_agent/check.sh . /expect_zabbix/check. ...