博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IrregularGridCollectionView处理不定宽度的标签cell
阅读量:4309 次
发布时间:2019-06-06

本文共 8330 字,大约阅读时间需要 27 分钟。

IrregularGridCollectionView处理不定宽度的标签cell

 

效果

 

源码

中的 

////  IrregularGridCollectionView.h//  IrregularGridCollectionView////  Created by YouXianMing on 16/8/30.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import 
#import "IrregularGridCellDataAdapter.h"#import "MaximumSpacingFlowLayout.h"#import "CustomIrregularGridViewCell.h"@class IrregularGridViewCellClassType;@class IrregularGridCollectionView;@protocol IrregularGridCollectionViewDelegate
@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
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
*registerCells;/** * The cells data adapter. */@property (nonatomic, strong) NSMutableArray
*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
)delegate registerCells:(NSArray
*)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;@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 () 
@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)frame delegate:(id
)delegate registerCells:(NSArray
*)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
*)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

你可能感兴趣的文章
[development][profile][dpdk] KK程序性能调优
查看>>
GMF学习系列(二) 一些知识点(续2)
查看>>
jquery关于多个显示隐藏
查看>>
asp.net core中使用log4net
查看>>
c++ STL deque容器成员函数
查看>>
LeetCode Contains Duplicate (判断重复元素)
查看>>
SVN安装部署
查看>>
MPU6050开发 -- 卡尔曼滤波(转)
查看>>
Redis主从实战
查看>>
plsql if
查看>>
LuoGu P2002 消息扩散
查看>>
linux 下安装JDK
查看>>
简单的ASP.NET无刷新分页
查看>>
宏定义学习
查看>>
omitting directory `folder/'
查看>>
JavaScript面试题
查看>>
TCollector
查看>>
我的博客网站开发6——博文关键字搜索
查看>>
vim7.1在windows下的编码设置[转]
查看>>
同步器之Exchanger
查看>>