這里利用heightForRowAtIndexPath:方法計(jì)算不等高cell的高度,在使用這個(gè)方法之前要明確這個(gè)方法的調(diào)用時(shí)間以及調(diào)用次數(shù):
- 1.每當(dāng)reloadData時(shí),有多少條數(shù)據(jù),就會(huì)調(diào)用多少次這個(gè)方法(比如一共有80條數(shù)據(jù),就會(huì)調(diào)用80次這個(gè)方法)
- 2.每當(dāng)有cell出現(xiàn)時(shí),就會(huì)調(diào)用一次這個(gè)方法
//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cellHeight;
}
-
用于描述cell的xib
xib.png
計(jì)算不等高cell高度的第一種方法:估算高度
- 1.在viewDidLoad方法中設(shè)置cell的估算高度
(void)viewDidLoad {
[super viewDidLoad];
//設(shè)置估算高度
self.tableView.estimatedRowHeight = 170;
}
- 2.在heightForRowAtIndexPath:方法中計(jì)算cell的高度
//定義間距
#define WYLMargin 10
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//獲取對(duì)應(yīng)的模型
WYLTopicItem *item = self.topicItemArray[indexPath.row];
//定義cell的高度
CGFloat cellHeight = 0;
//圖片+間距
cellHeight += WYLMargin + 35 + WYLMargin;
//label的高度
CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
CGFloat labelHeight = [item.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
cellHeight += labelHeight + WYLMargin;
//底部導(dǎo)航欄的高度
cellHeight += 35 + WYLMargin;
NSLog(@"heightForRowAtIndexPath-----%zd",indexPath.row);
return cellHeight;
}
- 3.利用估算高度計(jì)算cell高度的優(yōu)點(diǎn)和缺點(diǎn)
- 優(yōu)點(diǎn):
1.減少heightForRowAtIndexPath方法的調(diào)用次數(shù)
2.可以讓暫時(shí)看不見的cell的高度延遲計(jì)算 - 缺點(diǎn):
1.tableView的contentSize不太準(zhǔn)確
2.在tableView滑動(dòng)過程中,滾動(dòng)條的長(zhǎng)度會(huì)變來變?nèi)?可能會(huì)有跳躍效果
3.cell從緩存池中加載時(shí)又會(huì)重新計(jì)算它的高度
計(jì)算不等高cell高度的第二種方法:定義字典保存cell高度
- 1.定義存儲(chǔ)cell高度的字典
/** 存儲(chǔ)cell高度的字典*/
@property (nonatomic ,strong) NSMutableDictionary *cellHeightDict;
/** 懶加載存儲(chǔ)cell高度的字典*/
-(NSMutableDictionary *)cellHeightDict
{
if (_cellHeightDict == nil) {
_cellHeightDict = [NSMutableDictionary dictionary];
}
return _cellHeightDict;
}
- 2.在heightForRowAtIndexPath:方法中計(jì)算cell的高度
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//獲取對(duì)應(yīng)的模型
WYLTopicItem *item = self.topicItemArray[indexPath.row];
//用模型的地址作為字典的key
NSString *key = [NSString stringWithFormat:@"%p",item];
//取出模型對(duì)應(yīng)的cell的高度
CGFloat cellHeight = [self.cellHeightDict[key] doubleValue];
//如果cellHeight有值,就是cell的高度已經(jīng)計(jì)算過了,直接返回
if (cellHeight) return cellHeight;
//如果cell的高度沒有計(jì)算過,就計(jì)算cell高度
//圖片+間距
cellHeight += WYLMargin + 35 + WYLMargin;
//label的高度
CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
CGFloat labelHeight = [item.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
cellHeight += labelHeight + WYLMargin;
//底部導(dǎo)航欄的高度
cellHeight += 35 + WYLMargin;
//將計(jì)算過的cell高度保存到字典中
self.cellHeightDict[key] = @(cellHeight);
NSLog(@"heightForRowAtIndexPath----%zd",indexPath.row);
return cellHeight;
}
- 3.利用字典保存cell高度的優(yōu)缺點(diǎn)
- 優(yōu)點(diǎn):
當(dāng)cell重新加載到tableView中的時(shí)候,不會(huì)重復(fù)計(jì)算cell的高度 - 缺點(diǎn):
代碼太過繁瑣
計(jì)算不等高cell高度的第三種方法:在模型中定義cellHeight屬性
- 1.在模型.h文件中增加cellHeight屬性
#import <Foundation/Foundation.h>
@interface WYLTopicItem : NSObject
/** 根據(jù)當(dāng)前模型數(shù)據(jù)計(jì)算出來的cell高度*/
@property (nonatomic ,assign) NSInteger cellHeight;
@end
- 2.在模型.m文件中實(shí)現(xiàn)cellHeight的getter方法
#import "WYLTopicItem.h"
@implementation WYLTopicItem
-(NSInteger)cellHeight
{
//如果cellHeight已經(jīng)計(jì)算過,就直接返回
if (_cellHeight) return _cellHeight;
//圖片+間距
_cellHeight += WYLMargin + 35 + WYLMargin;
//label的高度
CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * WYLMargin, MAXFLOAT);
CGFloat labelHeight = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height;
_cellHeight += labelHeight + WYLMargin;
//底部導(dǎo)航欄的高度
_cellHeight += 35 + WYLMargin;
return _cellHeight;
}
@end
-3.在heightForRowAtIndexPath:方法中計(jì)算cell的高度
//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//獲取對(duì)應(yīng)的模型
WYLTopicItem *item = self.topicItemArray[indexPath.row];
return item.cellHeight;
}

效果圖.png
