通过NSCache缓存已经算好的行高

@interface ZHCellHeightCalculator : NSObject

//系统计算高度后缓存进cache
-(void)setHeight:(CGFloat)height withCalculateheightModel:(ZHCalculateHeightModel *)model; //根据model hash 获取cache中的高度,如过无则返回-1
-(CGFloat)heightForCalculateheightModel:(ZHCalculateHeightModel *)model; //清空cache
-(void)clearCaches; @end
#import "ZHCellHeightCalculator.h"

@interface ZHCellHeightCalculator ()
@property (strong, nonatomic, readonly) NSCache *cache;
@end @implementation ZHCellHeightCalculator #pragma mark - Init
-(instancetype)init
{
self = [super init];
if (self) {
[self defaultConfigure];
}
return self;
} -(void)defaultConfigure
{
NSCache *cache = [NSCache new];
cache.name = @"ZHCellHeightCalculator.cache";
cache.countLimit = ;
_cache = cache; } #pragma mark - NSObject - (NSString *)description
{
return [NSString stringWithFormat:@"<%@: cache=%@",
[self class], self.cache];
} #pragma mark - Publci Methods
-(void)clearCaches
{
[self.cache removeAllObjects];
} -(void)setHeight:(CGFloat)height withCalculateheightModel:(ZHCalculateHeightModel *)model
{
NSAssert(model != nil, @"Cell Model can't nil");
NSAssert(height >= , @"cell height must greater than or equal to 0"); [self.cache setObject:[NSNumber numberWithFloat:height] forKey:@(model.hash)];
} -(CGFloat)heightForCalculateheightModel:(ZHCalculateHeightModel *)model
{
NSNumber *cellHeightNumber = [self.cache objectForKey:@(model.hash)];
if (cellHeightNumber) {
return [cellHeightNumber floatValue];
}else
return -; }
@end

使用方式:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
ZHCalculateHeightModel *model = model = [dataArray objectAtIndex:indexPath.row]; CGFloat height = [heightCalculator heightForCalculateheightModel:model];
if (height>) {
NSLog(@"cache height");
return height;
}else{
NSLog(@"calculate height");
}
ZHCalculateTableViewCell *cell = self.prototypeCell;
cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[self configureCell:cell atIndexPath:indexPath];//必须先对Cell中的数据进行配置使动态计算时能够知道根据Cell内容计算出合适的高度 /*------------------------------重点这里必须加上contentView的宽度约束不然计算出来的高度不准确-------------------------------------*/
CGFloat contentViewWidth = CGRectGetWidth(self.tableView.bounds);
NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:contentViewWidth];
[cell.contentView addConstraint:widthFenceConstraint];
// Auto layout engine does its math
CGFloat fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
[cell.contentView removeConstraint:widthFenceConstraint];
/*-------------------------------End------------------------------------*/ CGFloat cellHeight = fittingHeight+*/[UIScreen mainScreen].scale;//必须加上上下分割线的高度
[heightCalculator setHeight:cellHeight withCalculateheightModel:model];
return cellHeight;
}

第二篇、为UITableViewCell 高度自适应加速 缓存cell的高度的更多相关文章

  1. winform中dataGridView高度自适应填充完数据的高度

    // winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...

  2. WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应

    WPF设置DataGrid行内容高度自适应  TextBox/TextBlock内容高度自适应  参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...

  3. 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】

    Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...

  4. Iframe 高度自适应,js控制Iframe 高度自适应

     Iframe 高度自适应, js控制Iframe 高度自适应, iframe自适应高度 ================================ ©Copyright 蕃薯耀 2019年12 ...

  5. textarea高度自适应,随着内容增加高度增加

    $(function(){        $.fn.autoHeight = function(){            function autoHeight(elem){             ...

  6. uitableviewcell高度自适应笔记

    今天看了几篇uitableviewcell高度自适应的文章,大体分为两种方式. 第一种方式,cell里面有label,在cellforrow绘制的时候计算Label的可能高度,并且在此时重新计算cel ...

  7. 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小

    上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...

  8. iOS开发之计算动态cell的高度并缓存

    项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...

  9. CSS技术让高度自适应减少很多不必要的检测

    高度自适应第一种情况 1.高度不去设置,或者高度设置auto 内容撑开父元素的高度.2.内容撑开父元素的高度 -> 最小高度的设置 min-height3.浮动元素添加高度自适应 -> 添 ...

随机推荐

  1. mysql xtarbackup备份脚本

    #!/bin/sh # # # Script config User="user" Password="passwd" Basedir=/application ...

  2. Hadoop on Mac with IntelliJ IDEA - 4 制作jar包

    本文讲述使用IntelliJ IDEA打包Project的过程,即,打jar包. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.4, Hadoop 1.2.1 Hado ...

  3. Javascript Design Patterns - Js Class

    JavaScript is a class-less language, however classes can be simulated using functions. eg: // A car ...

  4. cdoj 574 High-level ancients dfs序+线段树

    High-level ancients Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...

  5. C#实现汉字转换为拼音缩写的代码

    using System; using System.Configuration; using System.Data; using System.Web; using System.Web.Secu ...

  6. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  7. WIN8重见开始菜单

    从win7进入win8感觉不适应,做为一个程序开发人员,更觉得不爽,因此想着如何恢复开始菜单,查了很多文章终于找到方法 首先,在桌面版Windows 8启动资源管理器,单击工具栏上的“查看”选项卡后, ...

  8. iOS开发——UI_swift篇&UITableView实现索引功能

    UITableView实现索引功能     关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...

  9. LINUX 内核文档地址

    Linux的man很强大,该手册分成很多section,使用man时可以指定不同的section来浏览,各个section意义如下: 1 - commands2 - system calls3 - l ...

  10. c#线程问题(2)

    表示要在新上下文中调用的方法.public delegate void ContextCallback(Object state) 参数 state 类型: System. Object一个对象,包含 ...