首页 > 移动平台 > 详细

iOS tableView高度缓存

时间:2018-08-20 11:10:54      阅读:255      评论:0      收藏:0      [点我收藏+]

tableView计算完高度后,把高度缓存起来,避免下次重复计算,以减少不必要的消耗

// declare cellHeightsDictionary
NSMutableDictionary *cellHeightsDictionary;
 
// initialize in code (thanks to @Gerharbo)
cellHeightsDictionary = @{}.mutableCopy;
 
// declare table dynamic row height and create correct constraints in cells
tableView.rowHeight = UITableViewAutomaticDimension;
 
// save height
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
}
 
// give exact height value
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *height = [cellHeightsDictionary objectForKey:indexPath];
    if (height) return height.doubleValue;
    return UITableViewAutomaticDimension;
}

iOS tableView高度缓存

原文:https://www.cnblogs.com/qqcc1388/p/9504295.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!