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E7eW9v75lwi2TvUk+UY28Lt3/APFehMfZDLhRtkVAASCGGZLR+DjNXdyYR5Mq5aXZo2vlBOpTmvxQs/8Ai/8AJqzpnWNZ9B0alKLaleLls2fOKbOYYmkoycVzaPVYe/pJMxr/AM3otXSy5+AhTT4+S/c9ZZZkSj/74nW2L49MvjTipfyWyuKSysTGrsNSyunlfn0ZRTd/vxsUyVntdL/fmIKG3pmxOzjDkjYcNrTNK0oKXiu6eWK1krSuo2gvDN+r+hhpRKUxiODTHvRkWZVkm+z1r16lTOc5S6tv5XsCggZ+nD9C7kz/2Q==)
本来上传了一张tableView的图片到百度,没想到百度居然说只限百度用户内部交流使用,靠。
先说一下刚开始的实现思路吧,一开始使用的Xib+AutoLayout,以及iOS8之后的自动估算高度。也就是设置
self.ableView.estimatedRowHeight = xxx;
self.tableView.rowHeight = UITableViewAutomaticDimension;
选择这种实现方式非常适合快速开发,快速的拖拽控件就可以很快的看见效果。
如果能这么顺利的解决问题,那就不会有这篇博客了。我们遇到的问题是,当切换到其他tabbarItem时,切换回首页之后,滑动tableView之后发现掉帧,而且很严重。
这种问题其实是使用预估高度必然会出现的问题,预估的高度和实际高度不一致,当cell滑动时,会调用很多次计算高度的方法,不停的在计算,fps必然会低于60,也就产生了用户所看到的跳帧。
解决思路:
- 缓存高度。
- self.ableView.estimatedRowHeight的值设置屏幕最终显示cell中实际高度最低的那个的高度值。
既然要缓存高度,首先需要得到cell的真实高度
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
[self.heightDict setObject:@(cell.height) forKey:key];
DEBUG_LOG(@"第%@行的计算的最终高度是%f",key,cell.height);
}
didEndDisplayingCell是cell已经被真正的显示在了屏幕上,所以在这里打印出的高度必然是最正确的高度。根据indexPath.row作为key,将高度缓存进字典。拿字典做容器的好处还有就是取出速度更快。底层是用hash表实现的。如果有人对hash表想深入研究的话,推荐看这篇文章。
下面就是如果字典里有值,则使用缓存高度,否则自动计算。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
if (self.heightDict[key] != nil) {
NSNumber *value = _heightDict[key];
DEBUG_LOG(@"%@行的缓存下来的高度是%f",key,value.floatValue);
return value.floatValue;
}
return UITableViewAutomaticDimension;
}
最后提醒一句:设置cell的预估高度时一定要设置最小高度cell的那个值。不然的话,在滑动的时候,当高度最小的那个滑动到一大半的时候,就会突然一下消失,造成掉帧。对于这个问题我并没有深入的去研究,但是把预估高度设置成最小的那个高度就可以解决这个问题。如果有人知道的话可以在底下留言。
喜欢的话就点个吧。