您的位置:首页 > 产品设计 > UI/UE

iOS开发——UITableView优化之缓存cell高度

2017-07-05 17:22 591 查看

为什么要缓存高度?

因为当tableView滚动时会不停的回调
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath;
这个代理方法,当cell的高度需自适应内容时,就意味着每次回调这个方法时都要计算高度,而计算是要花时间了,在用户体验上的体现就是卡顿。为了避免重复且无意义的计算cell高度,缓存高度就显得尤为重要了。

怎样缓存?

缓存高度需要一个可变数组,每当回调
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法时,我们先去这个数组里去取,如果有,就直接拿出来,如果没有,就计算高度,并且放进数组。

代码讲解

这是我们用来装缓存高度的可变数组
/** 缓存cell高度的数组 */
@property (nonatomic,strong) NSMutableArray *heightArray;
注意懒加载
- (NSMutableArray *)heightArray{
if (_heightArray == nil) {
_heightArray = [NSMutableArray array];
}
return _heightArray;
}
缓存高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

CGFloat height;

if (self.heightArray.count > indexPath.row) {
// 如果有缓存的高度,取出缓存高度
height = [self.heightArray[indexPath.row] floatValue];;
}else{
// 无缓存高度,计算高度,并加入数组

// 高度根据评论内容多少自适应
CQGoodsCommentModel *model = self.dataArray[indexPath.row];
// 列寬
CGFloat contentWidth = screenWidth-20;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 计算size
CGSize size = [model.comment_content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];

// 這裏返回需要的高度
height = size.height+60;
// 加入数组
[self.heightArray addObject:[NSNumber numberWithDouble:height]];
}
return height;
}
刷新tableView时记得清空高度缓存数组
// 设置表头
self.contentTableView.header = [CQHeader headerWithRefreshingBlock:^{
_page = 1;
[self.heightArray removeAllObjects];
[self getDataIsRefresh:YES];
}];

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: