zoukankan      html  css  js  c++  java
  • iOS tableView高度缓存

    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;
    }
    
    
  • 相关阅读:
    elastic
    原生js获取css样式和修改css样式
    React项目开发中的数据管理
    js获取鼠标位置
    闭包
    HTML5与HTML4的区别
    JSON 相关
    RESTful Web Services初探
    IE6浏览器兼容问题及部分解决方案
    关于Doctype
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/9504295.html
Copyright © 2011-2022 走看看