原理与前一篇OC-UITableView快速注册获取Cell差不多 但是必须要注册才能使用
Swift版
.h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UICollectionViewCell (HWCategory)
/**
快速获取注册cellid
@return 注册的类名+ID
*/
+ (NSString *)hw_identifier;
@end
@interface UICollectionView (HWCategory)
/**
快速注册cell 根据传入的cellName自动判断是注册的xib还是Class
@param cell cell的class 或者cell字符串
*/
- (void)hw_registerCell:(id)cell;
/**
快速获取cell
@param cell cell的class 或者cell字符串
@param indexPath 对应的NSIndexPath
@return cell
*/
- (id)hw_dequeueReusableCell:(id)cell and:(NSIndexPath *)indexPath;
/**
快速注册头部
@param view view的class 或者view字符串
*/
- (void)hw_registerCollectionHeaderView:(id)view;
/**
快速返回头部
@param view view的class 或者view字符串
@param indexPath indexPath 对应的NSIndexPath
@return UICollectionReusableView
*/
- (id)hw_dequeueCollectionHeaderView:(id)view and:(NSIndexPath *)indexPath;
/**
快速注册尾部
@param view view的class 或者view字符串
*/
- (void)hw_registerCollectionFooterView:(id)view;
/**
快速返回尾部
@param view view的class 或者view字符串
@param indexPath indexPath 对应的NSIndexPath
@return UICollectionReusableView
*/
- (id)hw_dequeueCollectionFooterView:(id)view and:(NSIndexPath *)indexPath;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "UICollectionView+HWCategory.h"
@implementation UICollectionViewCell (HWCategory)
+ (NSString *)hw_identifier {
return [NSString stringWithFormat:@"%@ID",NSStringFromClass([self class])];
}
@end
@implementation UICollectionView (HWCategory)
// MARK: 注册cell
- (void)hw_registerCell:(id)cell {
NSString *cellName = [self getCellName:cell];
NSString *reuseIdentifier = [NSString stringWithFormat:@"%@ID", cellName];
if ([self hw_getNibPath:cellName]) {
[self registerNib:[UINib nibWithNibName:cellName bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
} else {
[self registerClass:NSClassFromString(cellName) forCellWithReuseIdentifier:reuseIdentifier];
}
}
// MARK: 获取cell
- (id)hw_dequeueReusableCell:(id)cell and:(NSIndexPath *)indexPath {
NSString *cellName = [self getCellName:cell];
NSString *reuseIdentifier = (NSString *)[NSString stringWithFormat:@"%@ID", cellName];
return [self dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
}
- (NSString *)getCellName:(id)cell {
NSString *cellName;
if ([cell isKindOfClass:[NSString class]]) {
cellName = cell;
} else {
cellName = NSStringFromClass(cell);
}
return cellName;
}
- (BOOL)hw_getNibPath:(NSString *)name {
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"nib"];
if (path == nil) { // 路径不存在
return NO;
} else { // 路径存在
return YES;
}
}
- (void)hw_registerCollectionHeaderView:(id)view {
NSString *viewName = [self getCellName:view];
NSString *reuseIdentifier = (NSString *)[NSString stringWithFormat:@"%@ID", viewName];
if ([self hw_getNibPath:viewName]) { // nib
[self registerNib:[UINib nibWithNibName:viewName bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifier];
} else { // class
[self registerClass:NSClassFromString(viewName) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifier];
}
}
- (id)hw_dequeueCollectionHeaderView:(id)view and:(NSIndexPath *)indexPath {
NSString *viewName = [self getCellName:view];
NSString *reuseIdentifier = (NSString *)[NSString stringWithFormat:@"%@ID", viewName];
return [self dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
}
- (void)hw_registerCollectionFooterView:(id)view {
NSString *viewName = [self getCellName:view];
NSString *reuseIdentifier = (NSString *)[NSString stringWithFormat:@"%@ID", viewName];
if ([self hw_getNibPath:viewName]) { // nib
[self registerNib:[UINib nibWithNibName:viewName bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseIdentifier];
} else { // class
[self registerClass:NSClassFromString(viewName) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseIdentifier];
}
}
- (id)hw_dequeueCollectionFooterView:(id)view and:(NSIndexPath *)indexPath {
NSString *viewName = [self getCellName:view];
NSString *reuseIdentifier = (NSString *)[NSString stringWithFormat:@"%@ID", viewName];
return [self dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
}
@end
网友评论