UITableView-MDKAutoLayoutHeight 1.1.3

UITableView-MDKAutoLayoutHeight 1.1.3

mikun维护。



  • 作者:
  • miku1958

UITableView-MDKAutoLayoutHeight

为 UITableview 提供高性能、低侵入性的高度计算和缓存工具

如果喜欢这个工具,请给我点个赞

如果你喜欢这个工具,请点一下星星支持

这是什么?

这是一个完全自动且基本无侵入性的 UITableview 高度计算和缓存工具

功能

  • 高性能:尽可能减少对 cell 高度的重新计算,提供内存和磁盘缓存。
  • 自动更新:当 cell 的内容或 tableview 的宽度根据 dataModel 的哈希值更改时,它会自动更新。
  • 内存管理:当系统通知内存不足时,自动清除内存。
  • 低侵入性:基本上不需要更改任何代码结构即可使用此库,不同于 FDTemplateLayoutCell。
  • 轻量级:此库的核心仅包含 1 个文件、1 个类和 1 个扩展。
  • 易于使用:只需一行代码即可享受完全自动的高度计算。

功能

  • 高性能:尽可能减少计算cell的高度,并提供内存和磁盘缓存。
  • 自动更新:基于数据模型hash的更新,当数据源或tableview宽度变化时会自动更新缓存。
  • 内存管理:当系统提示内存不够时自动清理内存缓存。
  • 低侵入性:基本上无需更改任何代码结构就能使用这个框架,与FDTemplateLayoutCell不同,FDTemplateLayoutCell需要根据它们的设计修改大量原有代码(而且不好用)。
  • 轻量级:这个库的核心组件只有一个文件、一个类和一个tableview分类。
  • 容易使用:只需一行代码就能享受完全自动化的高度计算。

中文文档:

使用方法

#import "UITableView+MDKAutoLayoutHeight.h"

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return [tableView.autoLayoutHeight heightForRowAtIndexPath:indexPath];
}

就是这样!简单,对吧?

如果你需要处理结果高度,可以使用这个

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return [tableView.autoLayoutHeight heightForRowAtIndexPath:indexPath cacheKey:acacheKey handle:^CGFloat(__kindof UITableViewCell *cell, CGFloat height) {
	   return height + 20;
   }];
}

如果你的cell在contentView中没有完全约束(不是一个自适应的cell),可以使用MDKAutoLayoutRegisterHeight告诉我哪个视图位于底部,如下所示

#import "UITableView+MDKAutoLayoutHeight.h"

@interface PartOfAutolayoutCell()
@proterty(nonatomic,strong)UIView *bottomView;
@end

@implementation PartOfAutolayoutCell
+(void)initialize{
	MDKAutoLayoutRegisterHeight(self, bottomView)
}
@end

如果你实现了这个,它也适用于那些在layoutSubview中或使用框架来配置cell布局的cell,或在其他神奇方法(我并不完全确定)中,如果你发现你在某个地方设置了框架,而这个工具不起作用,请告诉我位置在哪里

MDKAutoLayoutRegisterHeight() 是基于C语言宏,如果你使用Swift,可以使用MDKAutoLayoutHeight.(registerHeight:_decisionView:#keyPath(view.bottom))来填充视图的属性名称

在RAM中缓存cell的高度

如果heightForRowAtPath的acacheKey为nil或@“”,则需要遵循以下代码来返回缓存键

在您的单元格中引入 <MDKTableviewCellCacheHeightDelegate>,实现 -MDKModelHash method,返回一些唯一的东西,如 dataModel 的 ID,例如:

-(NSString *)MDKModelHash{
	return @(_model.ID).description;
}

如果您不是在单元格中处理模型,而是在 VieController 中,您可以轻松将模型 ID 传输到单元格并返回它等

如果您的单元格可能更改 dataModel 的内容,您可以返回 ID 和一些标识符,如

-(NSString *)MDKModelHash{
	return [NSString stringWithFormat:@"%@%@",@(_model.ID),@(_model.isDelete)];
}

等等...

安装

pod 'UITableView-MDKAutoLayoutHeight'

如果您需要将高度缓存到磁盘和 RAM

pod 'UITableView-MDKAutoLayoutHeight/diskCache'

当 tableview 被销毁时,缓存将写入磁盘
我提供了这些方法来管理磁盘的缓存

导入 UITableView+MDKAutoLayoutHeightDiskCache.h 以便于更改,现在不需要更改头文件了

- (void)updateDiskCache;//use to those tableview that is always alive
- (void)removeCacheFor:(Class)cell;
- (void)removeAllCache;

已知问题

如果您以这种方式对单元格进行队列管理:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

	return [tableView dequeueReusableCellWithIdentifier:@"Identifier" forIndexPath:indexPath];	
}

它将崩溃,因为我使用此数据源方法获取单元格,而 -dequeueReusableCellWithIdentifier:forIndexPath: 将调用 table.delegate -heightForRowAtIndexPath,因此将进入无限循环然后崩溃,解决方案是使用 table -dequeueReusableCellWithIdentifier: 而不是 forIndexPath 一个。我找不到需要使用此方法的原因,如果您必须使用此方法对单元格进行队列管理,请告诉我原因,我将尝试找到一种避免的方法

感谢

我使用的某些代码来确定 contentView 的宽度来自 UITableView-FDTemplateLayoutCell