zoukankan      html  css  js  c++  java
  • iOS | TableView的优化

    TableView是iOS组件中最常见、最重要的组件之一,在开发中常常用到,所以对其进行优化是一项必不可少的基本功。

    主要从几个最常用的方面来对其优化:

    1.重用机制
    重用机制是cell最基础的一项优化手段,通过几个方法的组合来实现。

    //注册cell或者head、foot
    - (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
    - (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
    
    - (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
    - (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
    
    //从缓存池中获取重用对象
    - (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
    - (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
    - (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);  // like dequeueReusableCellWithIdentifier:, but for headers/footers
    

    另外,尤其要注意的是,在拖动中,因为table会不停的对缓存池中的cell进行存取操作,所以不要动态的去创建或者删除控件。最好是一开始创建好所有需要的控件,然后根据条件去进行显示或者隐藏。


    2.提前准备数据
    一定要提前准备好数据,在tableView显示前,把模型数据加载好,然后对应赋值给cell,避免去边显示边加载的操作。


    3.cell高度
    在tableView滑动的过程中,

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    会调用的非常频繁。如果不是使用预估行高去处理的话,建议可以在模型中添加一个行高属性,在网络加载数据后对控件的高度进行统一计算,然后通过该方法去返回,并且设置只计算一次,保存在缓存中,如果模型行高属性有值,则不再计算。


    4.处理图片
    图片处理很重要,如果直接使用图片加载上去,可能会很严重的影响性能。这里有两种方案去处理,其一是拿到图片之后进行裁剪成合适的图片再放上去,其二是让后台直接将处理好的图片返回给你使用。


    5.避免透明色
    GPU对透明的控件会有性能浪费,所以可能的情况下,要保证所有的控件的背景色不是透明的。

  • 相关阅读:
    Eclipse快捷键 10个最有用的快捷键
    Eclipse--Web项目中 .classpath、mymetadata、project文件的功用
    java.lang.IllegalStateException:Web app root system property already set to different value 错误原因及解决 Log4j
    验证位置时发生错误:“org.tigris.subversion.javahl.ClientException......
    隐藏控制台黑窗口
    APK伪加密
    格蠹汇编-01-blog
    static_cast、dynamic_cast、const_cast和reinterpret_cast总结
    CONTAINING_RECORD宏
    explicit关键字
  • 原文地址:https://www.cnblogs.com/JanChuJun/p/10102238.html
Copyright © 2011-2022 走看看