由于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 = ;
self.myTableView.rowHeight = UITableViewAutomaticDimension;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
04-25 23:20