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…

kafka java api 删除_使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)...

使用Kafka的同学都知道&#xff0c;我们每次创建Kafka主题(Topic)的时候可以指定分区数和副本数等信息&#xff0c;如果将这些属性配置到server.properties文件中&#xff0c;以后调用Java API生成的主题将使用默认值&#xff0c;先改变需要使用命令bin/kafka-topics.sh --zook…

操作系统: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家庭版 一般来说&…

mysql-5.5.8_MySQL5.5.8安装

一、软件包a) cmake-2.8.3.tar.gzb) mysql-5.5.8.tar.gz二、安装步骤a) Tar zxvf cmake-2.8.3.tar.gzb) Cd cmake-2.8.3c) ./bootstrapd) Makee) Make installf) Tar zxvf mysql-5.5.8.tar.gzg) Cd mysql-5.5.8h) 配置参数cmake . -DCMAKE_INSTALL_PREFIX/usr/local/mysql5 \-D…

C# Web实时消息后台服务器推送技术-GoEasy

越来越多的项目需要用到实时消息的推送与接收&#xff0c;怎样用C#实现最方便呢&#xff1f;我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台&#xff0c;使用它的API可以轻松搞定实时推送&#xff01; 浏览器兼容性&#xff1a;GoEasy推送 支持websocket 和polling两种…

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

一、打印时不出墨症状 打印机在联机或自检时&#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…

oralce之存储过程

一&#xff1a;--循环向表emp中插入数据 1 declare 2 maxnumber number:10000;3 v_count number;4 begin5 v_count :0;6 FOR x IN 1..maxnumber7 LOOP8 v_count :v_count1;9 insert into emp (empno,ename,job,mgr,sal,comm) 10 valu…

java 日期calendar_java时间对象Date,Calendar和LocalDate/LocalDateTime

一、简介Date&#xff1a;java.util.Date包&#xff0c;包含日期&#xff0c;时间&#xff0c;毫秒数。Calendar&#xff1a;java.util.Calendar包&#xff0c;abstract修饰&#xff0c;Date的很多方法已经过时迁移到了Calendar类上。LocalDate/LocalDateTime&#xff1a;java.…

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;那么我们有没有什么办法来简…