iOS 中 各种 Cell 和 HeaderView 复用

最初版(见名知意)

#define IdentifierForReusableHeaderCode + (NSString *)identifierForReusable;\
- (NSString *)identifierForReusable;

@interface UITableViewCell (IdentifierForReusable)

IdentifierForReusableHeaderCode

@end

@interface UICollectionReusableView (IdentifierForReusable)

IdentifierForReusableHeaderCode

@end

@interface UITableViewHeaderFooterView (IdentifierForReusable)

IdentifierForReusableHeaderCode

@end
复制代码

可以直接通过 Cell 的实例或者类名来直接访问一个固定的 identifier ,省去了 宏定义 cell 的 identifier 过程

随后

我想,既然省掉了声明或者宏定义 identifier 的过程,不如顺便把注册和获取的过程也简化下,于是便有了下面的东西

@interface UITableView (IdentifierForReusable)

/**
 使用类名注册 Cell
 */
- (void)registerCellClass:(Class)cellClass;
/**
 使用类名注册 Header Footer
 */
- (void)registerHeaderFooterClass:(Class)aClass;

- (__kindof UITableViewCell *)dequeueReusableCellWithClass:(Class)cellClass;

- (__kindof UITableViewCell *)dequeueReusableCellWithClass:(Class)cellClass forIndexPath:(NSIndexPath *)indexPath;

- (__kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithClass:(Class)aClass;

@end


@interface UICollectionView (IdentifierForReusable)
/**
 使用类名注册 Cell
 */
- (void)registerClass:(Class)cellClass;
/**
 使用类名注册 Header Footer
 */
- (void)registerClass:(Class)aClass forSupplementaryViewOfKind:(NSString *)kind;

/**
 获取 cell

 @param cellClass cell 的 class
 */
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseClass:(Class)cellClass forIndexPath:(NSIndexPath *)indexPath;

/**
 获取 UICollectionReusableView
 */
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseClass:(Class)aClass forIndexPath:(NSIndexPath *)indexPath;

@end

复制代码

举个栗子

  1. cell 和 headerfooter view 的注册
[_tableView registerCellClass:[RedTableViewCell class]];
[_tableView registerCellClass:[GreenTableViewCell class]];
[_tableView registerHeaderFooterClass:[SectionHeaderView class]];
[_tableView registerHeaderFooterClass:[SectionFooterView class]];
复制代码
  1. cell 的获取
// 支持原生方法 
RedTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[RedTableViewCell identifierForReusable] forIndexPath:indexPath];
// 简单方法 1
RedTableViewCell * cell = [tableView dequeueReusableCellWithClass:[RedTableViewCell class]];
// 简单方法 2
RedTableViewCell * cell = [tableView dequeueReusableCellWithClass:[RedTableViewCell class] forIndexPath:indexPath];
复制代码

3.headerfooter view

// 支持原生方法 
//    SectionHeaderView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[SectionHeaderView identifierForReusable]];
// 简单方法
SectionHeaderView * headerView = [tableView dequeueReusableHeaderFooterViewWithClass:[SectionHeaderView class]];
复制代码

Github 地址 觉得有用,请点一个 Star

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值