zoukankan      html  css  js  c++  java
  • 第四十五篇、UITableViewCell高度计算

    由于tableView:heightForRowAtIndexPath:方法的调用频率非常高,如果将cell高度的计算过程放在此方法中,那么效率将会非常的低,快速tableview就会出现卡顿

    1、通过代码

    (在模型当中只计算一次cell高度,然后在方法中直接从模型属性当中取出cell高度)

    #import <UIKit/UIKit.h>
    
    @interface CellItem : NSObject
    
    /**cell高度*///表明不能在外部修改
    @property (nonatomic, assign,readonly)  CGFloat cellHeight;
    
    @end
    #import "CellItem.h"
    
    @interface CellItem()
    {
        CGFloat _cellHeight;//使用了readonly策略,又实现了getter方法,编译器将不再生成_cellHeight成员变量,需要手动添加
    }
    @end
    
    @implementation CellItem
    
    - (CGFloat)cellHeight
    {
        if (!_cellHeight)//保证只计算一次
        {
            _cellHeight = /**计算cell高度*/
        }
        return _cellHeight;
    }
    
    @end

    2、通过自动布局,自动计算

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.myTableView.estimatedRowHeight = 44;
        self.myTableView.rowHeight = UITableViewAutomaticDimension;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewAutomaticDimension;
    }
  • 相关阅读:
    852. Peak Index in a Mountain Array
    841. Keys and Rooms
    832. Flipping an Image
    821. Shortest Distance to a Character
    824. Goat Latin
    如何生成git的公钥和私钥
    学习笔记
    加快JavaScript加载和执行效率
    PO BO VO DTO POJO DAO概念及其作用
    jvm 垃圾回收区的形象说明
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5928820.html
Copyright © 2011-2022 走看看