第二篇、为UITableViewCell 高度自适应加速 缓存cell的高度
通过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的高度的更多相关文章
- winform中dataGridView高度自适应填充完数据的高度
// winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...
- WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
WPF设置DataGrid行内容高度自适应 TextBox/TextBlock内容高度自适应 参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...
- 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】
Iframe的强大功能偶就不多说了,它不但被开发人员经常运用,而且黑客们也常常使用它,总之用过的人知道它的强大之处,但是Iframe有个致命的“BUG”就是iframe的高度无法自动适应,这一点让很多 ...
- Iframe 高度自适应,js控制Iframe 高度自适应
Iframe 高度自适应, js控制Iframe 高度自适应, iframe自适应高度 ================================ ©Copyright 蕃薯耀 2019年12 ...
- textarea高度自适应,随着内容增加高度增加
$(function(){ $.fn.autoHeight = function(){ function autoHeight(elem){ ...
- uitableviewcell高度自适应笔记
今天看了几篇uitableviewcell高度自适应的文章,大体分为两种方式. 第一种方式,cell里面有label,在cellforrow绘制的时候计算Label的可能高度,并且在此时重新计算cel ...
- 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小
上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...
- iOS开发之计算动态cell的高度并缓存
项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...
- CSS技术让高度自适应减少很多不必要的检测
高度自适应第一种情况 1.高度不去设置,或者高度设置auto 内容撑开父元素的高度.2.内容撑开父元素的高度 -> 最小高度的设置 min-height3.浮动元素添加高度自适应 -> 添 ...
随机推荐
- U3D中的协同等待函数
WaitForSeconds.WaitForseconds等待函数; 创建一个yield指令,来等待给定的秒数; using UnityEngine; using System.Collections ...
- [ALGO-3] K好数
算法训练 K好数 时间限制:1.0s 内存限制:256.0MB 问题描写叙述 假设一个自然数N的K进制表示中随意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数 ...
- struts2属性Struts2中属性接收参数中文问题和简单数据验证
PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 一:如果表单提交数据中有中文时,尽量应用post方式. 需要在Struts. ...
- POJ 3522 Slim Span 最小差值生成树
Slim Span Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3522 Description Gi ...
- BZOJ 2208: [Jsoi2010]连通数 tarjan bitset
2208: [Jsoi2010]连通数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- UNIX基础知识之出错处理
当UNIX函数出错时,常常会返回一个负值,而且整型变量errno通常被设置为含有附加信息的一个值.例如,open函数如成功执行则返回一个非负文件描述符,如出错则返回-1.在open出错时,有大约15种 ...
- MongoDB笔记--安装篇
安装MongoDB 第一步:下载安装包:官方下载地址←单击此处,如果是win系统,注意是64位还是32位版本的,请选择正确的版本. 第二步:新建目录“D:\MongoDB”,解压下载到的安装包,找到b ...
- debian7编译内核
第一个步骤“配置内核”. 在这里,我比较建议在发行版默认的config的基础上再进行配置,这样 配置出的内核和发行版本身才会有更好的相容性.比如可以在运行“make menuconfig”之前执行命令 ...
- Logistic回归总结
原文:http://blog.csdn.net/dongtingzhizi/article/details/15962797 Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2 ...
- 实例源码--Android旋转式菜单(效果很炫)
下载源码 技术要点: 1.旋转式菜单功能实现 2.动画的应用 3.自定义控件的使用 ...... 详细介绍: 1. 旋转式菜单功能实现 本套例子通过自定义布局与动画的综合使用,实现了旋转式菜单效果 ...