第二篇、为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.浮动元素添加高度自适应 -> 添 ...
随机推荐
- usr/bin/ld: cannot find 错误解决方法
参考:http://blog.siyebocai.cn/20100324_5p424qs7.html 通常在软件编译时出现的usr/bin/ld: cannot find -lxxx的错误,主要的原因 ...
- Mysql分表教程
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法.目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不是 ...
- android150 笔记
1. 什么是Activity? 四大组件之一,一般的,一个用户交互界面对应一个activity,界面的容器. setContentView() ,// 要显示的布局 button.setOnclick ...
- 比较escape、encodeURI、encodeURIComponent
估计很多前端工程师并不清楚escape,encodeURI, encodeURIComponent的区别,也不知道什么时候该用哪个方法,以及这些方法为什么要被用到,下面我主要来阐述一下这三个方法的区别 ...
- Qt Quick实现的疯狂算数游戏
使用 Qt Quick 写了个小游戏:疯狂算数.支持 Windows 和 Android 两个平台. 游戏简单,但牵涉到下面你的 Qt Quick 主题: 自己实现一个按钮 自适应分辨率 国际化 QM ...
- javascript 十六进制与RGB颜色值的相互转换
http://www.zhangxinxu.com/wordpress/?p=646 http://www.zhangxinxu.com/wordpress/?p=646 -------------- ...
- And Then There Was One
http://poj.org/problem?id=3517 And Then There Was One Time Limit: 5000MS Memory Limit: 65536K Tota ...
- python-其他常用模块
本节大纲: 模块介绍 time &datetime模块 random shutil shelve xml处理 yaml处理 configparser hashlib subprocess lo ...
- 第一节:Maven 下载,安装和配置
Maven是Apache的一个产品所以要下载Maven的话可以到https://www.apache.org网站上下载 进入到APache这个网站后看一下几部操作 第一: 选择点击导航栏上面的proj ...
- Android数据库的使用
学了web好久没继续做,现在做android开发断断续续也近一年了,实习是android,现在毕业了工作也是android,但是对于数据库这块由于最近项目需要就研究了下其常用操作,这篇博客中的观点仅代 ...