前面我的一篇博文UITableViewCell 高度自適應擴展中說到了可以緩存
Cell
高度提高性能田巴!下面為大家講述如何添加緩存提高性能允耿!先看兩張用Time Profiler
測試的緩存前后的耗時對比:
重點看函數(shù)heightForRowAtIndexPath
沒加緩存前
timeProfile.gif
添加緩存后
recordTime2.gif
前后對比差距是巨大的,計算高度這里減少的時間大約是五到六倍
如何添加緩存品抽?
- 新建一個緩存高度的類取名:
ZHCellHeightCalculator
ZHCellHeightCalculator.h
代碼
@interface ZHCellHeightCalculator : NSObject
//系統(tǒng)計算高度后緩存進cache
-(void)setHeight:(CGFloat)height withCalculateheightModel:(ZHCalculateHeightModel *)model;
//根據(jù)model hash 獲取cache中的高度,如過無則返回-1
-(CGFloat)heightForCalculateheightModel:(ZHCalculateHeightModel *)model;
//清空cache
-(void)clearCaches;
@end
ZHCellHeightCalculator.m
代碼
#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 = 200;
_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 >= 0, @"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 -1;
}
@end
修改
ZHCustomLayoutTableViewController.m
中heightForRowAtIndexPath
方法如下:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
ZHCalculateHeightModel *model = model = [dataArray objectAtIndex:indexPath.row];
CGFloat height = [heightCalculator heightForCalculateheightModel:model];
if (height>0) {
NSLog(@"cache height");
return height;
}else{
NSLog(@"calculate height");
}
ZHCalculateTableViewCell *cell = self.prototypeCell;
cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[self configureCell:cell atIndexPath:indexPath];//必須先對Cell中的數(shù)據(jù)進行配置使動態(tài)計算時能夠知道根據(jù)Cell內(nèi)容計算出合適的高度
/*------------------------------重點這里必須加上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+2*1/[UIScreen mainScreen].scale;//必須加上上下分割線的高度
[heightCalculator setHeight:cellHeight withCalculateheightModel:model];
return cellHeight;
}