tableViewCell行高計(jì)算
UITableView 是在app界面里非常常用的一個(gè)控件了,打開一個(gè)app,內(nèi)容列表 作者列表 朋友圈列表等等,,,都離不開 UITableView 。 而 UITableView 的精髓,則是在 UITableViewCell 展現(xiàn)的, 最常用的 自定義cell 有的行高是固定的,而大部分 則需要根據(jù)內(nèi)容來計(jì)算行高展示的。
下面就說說我在實(shí)際開發(fā)中處理cell行高的幾種情況:
1. 不需要?jiǎng)討B(tài)計(jì)算高度
我在寫tableview時(shí),基本都是自定義cell,而所有的自定義cell,都會(huì)繼承一個(gè)基類BaseTableViewCell:
.h里:
// 重用標(biāo)識(shí)
+ (NSString *)reuseIdentifier;
// cell高度
+ (CGFloat)staticHeight;
.m里:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.opaque = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
// 重用標(biāo)識(shí)
+ (NSString *)reuseIdentifier {
return NSStringFromClass([self class]);
}
// cell高度
+ (CGFloat)staticHeight {
return 44.f;
}
這樣寫的好處是,當(dāng)我們在使用tableview時(shí),會(huì)方便我們對(duì)重用標(biāo)識(shí)符 行高使用,看一下:
staticHeight可以在子類的自定義cell里更改設(shè)置,使用時(shí):
這樣寫,更能清晰明了的看到對(duì)每個(gè)自定義cell的設(shè)置,也會(huì)讓代碼看上去優(yōu)雅整齊一些。
2. 自適應(yīng)高度
在 iOS8 之后,系統(tǒng)結(jié)合autolayout提供了動(dòng)態(tài)結(jié)算行高的方法 UITableViewAutomaticDimension,做好約束,我們都不用去實(shí)現(xiàn) heightForRowAtIndexPath 這個(gè)代理方法了。
masonry支持毫無壓力。
實(shí)現(xiàn)步驟:
- tableView設(shè)置
// 預(yù)設(shè)行高
self.tableView.estimatedRowHeight = xxx;
// 自動(dòng)計(jì)算行高模式
self.tableView.rowHeight = UITableViewAutomaticDimension;
- 在自定義cell里,masonry布局,比如:
布局時(shí)兩個(gè)注意點(diǎn):
· 所有子控件,都要依賴與self.contentView作為約束父控件,而不是self(cell)
· 關(guān)鍵控件要做bottom約束 (因?yàn)椴辉僦付ㄐ懈?,所以要需要給出根據(jù)bottom的約束)
-
最關(guān)鍵的一步: [cell layoutIfNeeded]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { WMDoctorEvaluateDescribeInputCell *cell = [tableView dequeueReusableCellWithIdentifier:[WMDoctorEvaluateDescribeInputCell reuseIdentifier] forIndexPath:indexPath]; kWeakSelf cell.describeInputBlock = ^(NSString * _Nonnull describeText) { weakSelf.inputDescribeText = describeText; }; //關(guān)鍵的一步,解決不正常顯示問題 [cell layoutIfNeeded]; return cell; }
這樣就完成了自動(dòng)適應(yīng)高度的要求了。
另外: 針對(duì)一些自動(dòng)適應(yīng)高度不好做的cell,可以單獨(dú)處理 如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2) {
return [WMDoctorEvaluateStarCell staticHeight];
}
return UITableViewAutomaticDimension;
}
5.自適應(yīng)高度 - 緩存行高
在用UITableViewAutomaticDimension,有的界面比較復(fù)雜,雖然這樣能完成顯示,但是在滑動(dòng)的過程中,能肉眼感受到卡 掉幀,眾所周知 60fps是比較符合人眼審視的,如果幀數(shù) 低于這個(gè)數(shù)值過多,就會(huì)明顯感受到卡幀等現(xiàn)象,這屬于優(yōu)化性能方面的問題,所以就要思考一下怎樣來達(dá)到優(yōu)化tableview性能。
思路: 緩存高度機(jī)制
首先獲取cell實(shí)際顯示的高度
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
[self.heightDict setObject:@(cell.height) forKey:key];
NSLOG(@"第%@行的計(jì)算的最終高度是%f",key,cell.height);
}
//didEndDisplayingCell 當(dāng)cell滑出屏幕時(shí)會(huì)觸發(fā)此方法,是cell已經(jīng)被真正的顯示在了屏幕上,所以在這里打印出的高度必然是最正確的高度。根據(jù)indexPath.row作為key,將高度緩存進(jìn)字典.
然后在 heightForRowAtIndexPath 方法里判斷,如果字典里有值,則使用緩存高度,否則自動(dòng)計(jì)算:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
if (self.heightDict[key] != nil) {
NSNumber *value = _heightDict[key];
return value.floatValue;
}
return UITableViewAutomaticDimension;
}
注意:設(shè)置cell的預(yù)估高度時(shí)一定要設(shè)置最小高度cell的那個(gè)值。不然的話,在滑動(dòng)的時(shí)候,當(dāng)高度最小的那個(gè)滑動(dòng)到一大半的時(shí)候,就會(huì)突然一下消失,造成掉幀的現(xiàn)象。