白羽
2018-07-16
来源 :网络
阅读 1487
评论 0
摘要:本文将带你了解iOS开发之定制通用的标签选择器,希望本文对大家学IOS有所帮助。
定制通用的标签选择器
效果
源码
https://github.com/YouXianMing/UI-Component-Collection 中的 CollectionGridView
//
// CollectionGridView.h
// CollectionView
//
// Created by YouXianMing on 16/7/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CollectionGridCellDataAdapter.h"
#import "CustomCollectionGridViewCell.h"
@class CollectionGridView;
@class CollectionGridViewCellClassType;
#pragma mark - CollectionGridView Class
@protocol CollectionGridViewDelegate <NSObject>
@optional
/**
* CollectionGridView did selected event.
*
* @param collectionGridView CollectionGridView's object.
* @param cell CustomCollectionGridViewCell type's cell.
*/
- (void)collectionGridView:(CollectionGridView *)collectionGridView didSelectedCell:(CustomCollectionGridViewCell *)cell;
@end
@interface CollectionGridView : UIView
/**
* CollectionGridView's delegate.
*/
@property (nonatomic, weak) id <CollectionGridViewDelegate> delegate;
/**
* 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;
/**
* The cell's count at the horizontal direction, default is 3.
*/
@property (nonatomic) NSUInteger horizontalCellCount;
/**
* Register the cells.
*/
@property (nonatomic, strong) NSArray <CollectionGridViewCellClassType *> *registerCells;
/**
* The cells data adapter.
*/
@property (nonatomic, strong) NSArray <CollectionGridCellDataAdapter *> *adapters;
/**
* To make the config effective.
*/
- (void)makeTheConfigEffective;
/**
* Reset the view's size.
*/
- (void)resetSize;
/**
* Get the cell's size.
*/
@property (nonatomic, readonly) CGSize cellSize;
/**
* Get the CollectionView's content size.
*/
@property (nonatomic, readonly) CGSize contentSize;
/**
* Reloads just the items at the specified index paths.
*
* @param indexPaths An array of NSIndexPath objects identifying the items you want to update.
*/
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
/**
* Reload data.
*/
- (void)reloadData;
@end
#pragma mark - CollectionGridViewCellClassType Class
@interface CollectionGridViewCellClassType : NSObject
@property (nonatomic) Class className;
@property (nonatomic, strong) NSString *reuseIdentifier;
@end
NS_INLINE CollectionGridViewCellClassType *gridViewCellClassType(Class className, NSString *reuseIdentifier) {
CollectionGridViewCellClassType *type = [CollectionGridViewCellClassType new];
type.className = className;
type.reuseIdentifier = reuseIdentifier;
return type;
}
//
// CollectionGridView.m
// CollectionView
//
// Created by YouXianMing on 16/7/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import "CollectionGridView.h"
@implementation CollectionGridViewCellClassType
@end
@interface CollectionGridView () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic) CGSize cellSize;
@property (nonatomic) CGSize contentSize;
@end
@implementation CollectionGridView
#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;
self.horizontalCellCount = 3;
// Init UICollectionViewFlowLayout.
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.flowLayout.minimumInteritemSpacing = 0;
// 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 {
CGFloat width = self.frame.size.width;
CGFloat cellWidth = (width - self.contentEdgeInsets.left -
self.contentEdgeInsets.right -
_horizontalGap * (_horizontalCellCount - 1)) / (CGFloat)_horizontalCellCount;
self.collectionView.contentInset = self.contentEdgeInsets;
self.flowLayout.minimumLineSpacing = self.verticalGap;
self.flowLayout.minimumInteritemSpacing = self.horizontalGap;
self.flowLayout.itemSize = CGSizeMake(cellWidth, self.gridHeight);
self.cellSize = self.flowLayout.itemSize;
}
#pragma mark - Reload data.
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
- (void)reloadData {
[self.collectionView reloadData];
}
#pragma mark - UICollectionView's delegate & data source.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _adapters.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionGridCellDataAdapter *adapter = _adapters[indexPath.row];
CustomCollectionGridViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:adapter.cellReuseIdentifier
forIndexPath:indexPath];
cell.dataAdapter = adapter;
cell.data = adapter.data;
cell.indexPath = indexPath;
cell.collectionView = collectionView;
cell.collectionGridView = self;
[cell loadContent];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCollectionGridViewCell *cell = (CustomCollectionGridViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
[cell selectedEvent];
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionGridView:didSelectedCell:)]) {
[self.delegate collectionGridView:self didSelectedCell:cell];
}
}
#pragma mark - Setter & Getter
- (void)setRegisterCells:(NSArray <CollectionGridViewCellClassType *> *)registerCells {
_registerCells = registerCells;
for (CollectionGridViewCellClassType *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
//
// CustomCollectionGridViewCell.h
// CollectionView
//
// Created by YouXianMing on 16/7/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CollectionGridCellDataAdapter.h"
@class CollectionGridView;
@interface CustomCollectionGridViewCell : UICollectionViewCell
@property (nonatomic, weak) id data;
@property (nonatomic, weak) CollectionGridCellDataAdapter *dataAdapter;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, weak) NSIndexPath *indexPath;
@property (nonatomic, weak) CollectionGridView *collectionGridView;
#pragma mark - Method you should overwrite.
/**
* Setup cell, override by subclass.
*/
- (void)setupCell;
/**
* Build subview, override by subclass.
*/
- (void)buildSubview;
/**
* Load content, override by subclass.
*/
- (void)loadContent;
/**
* Selected event, override by subclass.
*/
- (void)selectedEvent;
#pragma mark - Constructor.
+ (CollectionGridCellDataAdapter *)dataAdapterWithCellReuseIdentifier:(NSString *)reuseIdentifier
data:(id)data
type:(NSInteger)type;
@end
//
// CustomCollectionGridViewCell.m
// CollectionView
//
// Created by YouXianMing on 16/7/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import "CustomCollectionGridViewCell.h"
@implementation CustomCollectionGridViewCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupCell];
[self buildSubview];
}
return self;
}
- (void)setupCell {
}
- (void)buildSubview {
}
- (void)loadContent {
}
- (void)selectedEvent {
}
+ (CollectionGridCellDataAdapter *)dataAdapterWithCellReuseIdentifier:(NSString *)reuseIdentifier data:(id)data type:(NSInteger)type {
NSString *identifierString = nil;
reuseIdentifier.length <= 0 ? (identifierString = NSStringFromClass([self class])) : (identifierString = reuseIdentifier);
return [CollectionGridCellDataAdapter collectionGridCellDataAdapterWithCellReuseIdentifier:identifierString data:data cellType:type];
}
@end
//
// CollectionGridCellDataAdapter.h
// CollectionView
//
// Created by YouXianMing on 16/7/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CollectionGridCellDataAdapter : NSObject
@property (nonatomic, strong) id data;
@property (nonatomic, strong) NSString *cellReuseIdentifier;
@property (nonatomic, weak) NSIndexPath *indexPath;
@property (nonatomic) NSInteger cellType;
+ (instancetype)collectionGridCellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifier
data:(id)data
cellType:(NSInteger)cellType;
@end
NS_INLINE CollectionGridCellDataAdapter *collectionGridCellDataAdapter(NSString *cellReuseIdentifier, id data, NSInteger type) {
return [CollectionGridCellDataAdapter collectionGridCellDataAdapterWithCellReuseIdentifier:cellReuseIdentifier
data:data cellType:type];
}
//
// CollectionGridCellDataAdapter.m
// CollectionView
//
// Created by YouXianMing on 16/7/14.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import "CollectionGridCellDataAdapter.h"
@implementation CollectionGridCellDataAdapter
+ (instancetype)collectionGridCellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifier
data:(id)data
cellType:(NSInteger)cellType {
CollectionGridCellDataAdapter *adapter = [[self class] new];
adapter.cellReuseIdentifier = cellReuseIdentifier;
adapter.data = data;
adapter.cellType = cellType;
return adapter;
}
@end
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号