IrregularGridCollectionView处理不定宽度的标签cell

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/270553.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

服务端负载均衡和客户端负载均衡

服务端负载均衡 用户在App访问通过80端口请求nginx,ngin来实现负载均衡&#xff0c;分发请求 客户端负载均衡 Eureka Server注册中心集群部署&#xff0c;goods_services服务提供者启动后向Eureka Server注册中心进行服务注册 App服务从Eureka Server发现服务 goods_servic…

java上传和下载文件代码_JavaWeb中上传和下载文件实例代码

一丶先引入上传下载的lib二丶上传的的servletpackage com.test.action;import java.io.file;import java.io.fileoutputstream;import java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import java.util.list;import javax.servlet.servletexcept…

操作系统:Linux 环境变量配置的 6 种方法

目录 Linux环境变量配置 Linux读取环境变量 Linux环境变量配置方法一&#xff1a;export PATH Linux环境变量配置方法二&#xff1a;vim ~/.bashrc Linux环境变量配置方法三&#xff1a;vim ~/.bash_profile Linux环境变量配置方法四&#xff1a;vim /etc/bashrc Linux环境变量…

操作系统:Win10有哪些版本,看完你就知道了

目录 一、win10家庭版 二、win10专业版 三、win10企业版 四、win10教育版 Win10有四个版本是我们最常见的&#xff1a;win10家庭版、win10专业版、win10企业版、win10教育版。 今天就主要说这4个比较经典的版本&#xff0c;一起来看看吧&#xff01; 一、win10家庭版 一般来说&…

硬件知识:打印机常见的故障及维护,值得收藏

一、打印时不出墨症状 打印机在联机或自检时&#xff0c;打印头有动作&#xff0c;但打印不出墨。故障分析 这一情况发生&#xff0c;有多方面原因&#xff0c;可能包括喷头故障&#xff0c;清洁单元故障&#xff0c;电镀及电路板故障等&#xff0c;但在排除了喷头故障后可以这…

mac下SecureCRT连接阿里云服务器最新教程

一.首先进入自己的阿里云管理控制台 地址 https://ecs.console.aliyun.com/?spm5176.6660585.774526198.1.57c96bf8inrLvC#/home 二&#xff1a;输入密码 三&#xff1a;点击密码重置 四&#xff1a;打开SecureCRT 点击加号 五&#xff1a;点击continue 六&#xff1a;在Ho…

IIS实现服务器反向代理用法介绍

今天给打击分享IIS实现服务器反向代理用法&#xff0c;感兴趣的可以学习一下&#xff01;场景&#xff1a;本地电脑启动了两个网站地址分别为&#xff1a;http://127.0.0.1:8081/Sitehttp://127.0.0.1:8082/Test要实现同一个端口访问&#xff1a;http://127.0.0.1:8080/Sitehtt…

java if else过多_Spring Boot中如何干掉过多的if else!

需求这里虚拟一个业务需求&#xff0c;让大家容易理解。假设有一个订单系统&#xff0c;里面的一个功能是根据订单的不同类型作出不同的处理。订单实体&#xff1a;service接口&#xff1a;传统实现根据订单类型写一堆的if else&#xff1a;策略模式实现利用策略模式&#xff0…

硬件知识:固态硬盘相关知识介绍

目录 1、主控 2、固件算法 3、SSD的SATA接口与M.2接口 4、速度对比 今天就为大家全面科普一下固态硬盘的相关知识&#xff0c;让大家购买时做到心中有数&#xff0c;按需选择。 首先还是从SSD的结构来说起&#xff0c;SSD最基本的组成部件分为&#xff1a;主控芯片、闪存芯片、…

编码实战Web端联系人的增删改查

首先画出分析图 实现效果如图 项目下的包如图&#xff1a; 实体包 package com.contactSystem.entiey;public class Contact {private String Id;private String name;private String sex;private String age;private String phone;private String qq;private String email;pub…

选型java程序_Java程序员自动化指南

一、背景在Java web开发中&#xff0c;虽然Spring boot已经帮助我们简化了很多工作&#xff0c;但项目中庞杂的业务仍然需要自己去编写较多的 entity&#xff0c;vo&#xff0c;Mapper&#xff0c;Service&#xff0c; Controller 代码等&#xff0c;那么我们有没有什么办法来简…

网络知识:光猫光纤宽带故障排查笔记

在日常上网过程中出现的故障&#xff0c;很大一部分是由于线路和光猫故障引起&#xff0c;现简单介绍一下&#xff0c;如何处理这些故障。 现象一&#xff1a;不能上网&#xff08;网络中断&#xff09; 故障排查&#xff1a; 1、确认您的光猫信号灯是否正常&#xff1a; ①电源…

treeview自动从表中添加标题和列值做目录的方法2

treeview自动从表中添加标题和列值做目录的方法2&#xff0c;该方法是借鉴万一老师的 http://www.cnblogs.com/del/archive/2008/05/15/1114450.html 首先界面上添加treeview组件&#xff0c;然后在treeview的onchange事件里这样写&#xff1a; 因为要用到定义个过程&#xff0…

Linux常用运维命令笔记

今天给大家整理一下Linux常用的命令&#xff0c;希望对大家能有所帮助&#xff01;MYSQL相关1、查看mysql版本status; select version()2、 mysql启动命令#01 使用 service 启动&#xff1a;service mysqld start (5.0版本) service mysql start (5.5.7版本) #02 使用 mysqld 脚…