为什么要缓存高度?
因为当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]; }];
总结
总的来说缓存cell高度还是比较简单的,只需在原来的基础上多加一个可变数组即可,逻辑也不复杂。不过,其对性能的优化还是不容小觑的,尤其是cell内容复杂且需高度自适应内容时。强烈建议人人get这项技术,即使是入门级开发者。