UITableViewCell高度計算

由于tableView:heightForRowAtIndexPath:方法的調(diào)用頻率非常高,如果將cell高度的計算過程放在此方法中,那么效率將會非常的低,快速tableview就會出現(xiàn)卡頓

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策略,又實現(xiàn)了getter方法,編譯器將不再生成_cellHeight成員變量,需要手動添加
}
@end

@implementation CellItem

- (CGFloat)cellHeight
{
    if (!_cellHeight)//保證只計算一次
    {
        _cellHeight = /**計算cell高度*/
    }
    return _cellHeight;
}

@end
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellItem.cellHeight;
}

2、通過自動布局,自動計算

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.estimatedRowHeight = 44;
    self.myTableView.rowHeight = UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容