-------推薦使用最下方的【改進】方法-------
(前排補上一個簡單的小Demo:https://github.com/3KK3/AutoFitDemo/tree/master
時間倉促炼杖,有些許不足喉酌,敬請見諒厉碟。)
方法一:
對cell的subview增加針對于
contentView
的約束(注意設(shè)置文字的最大寬度);設(shè)置tableview預估高度
estimateHeight
在cell拿到數(shù)據(jù)的地方
cellForRowAtIndexPath
,進行強制刷新布局:先根據(jù)數(shù)據(jù)和cell控件要求計算實際高度,并存入dataModel模型(做緩存),然后調(diào)用方法[self layoutIfNeeded]
(第一次顯示內(nèi)容的時候 無需調(diào)用setNeedsLayout),這樣heightForIndexPath
會緊隨其后獲取cell的真實高度,此時存入模型的cell的高度就起了作用了
PS:
預估高度機制 啟用后 改變cell的加載順序
原來的順序 : 先調(diào)用
heightForRowAtIndexPath
獲取真實高度,拿到高度之后再調(diào)用cellForRowAtIndexPath
渲染cell內(nèi)容啟用預估高度之后:先調(diào)用
estimatedHeightForRowAtIndexPath
返回代理估計高度秕磷,然后再根據(jù)預估高度調(diào)用cellForRowAtIndexPath
渲染cell內(nèi)容,同時計算真實高度,最后再調(diào)用heightForRowAtIndexPath
拿到計算的真實高度 顯示
方法二:
對cell的subview增加針對于contentView的約束(注意設(shè)置文字的最大寬度)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//只創(chuàng)建一個cell用作測量高度
static MyCell *cell = nil;
if (!cell) cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell"];
//這里把數(shù)據(jù)設(shè)置給Cell
cell.titleLabel.text = [_dataSource objectAtIndex:indexPath.row];
[cell layoutIfNeeded];
[cell updateConstraintsIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
方法三:
1.在cell中設(shè)置多行l(wèi)abel的約束:
// 計算UILabel的preferredMaxLayoutWidth值,多行時必須設(shè)置這個值,否則系統(tǒng)無法決定Label的寬度
CGFloat preferredMaxWidth = [UIScreen mainScreen].bounds.size.width - (16 + 4) * 2 - 44 - 4;
// Content - 多行
_contentLabel = [UILabel new];
_contentLabel.numberOfLines = 0;
_contentLabel.preferredMaxLayoutWidth = preferredMaxWidth; // 多行時必須設(shè)置
[self.contentView addSubview:_contentLabel];
[_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_titleLabel.mas_bottom).with.offset(4);
make.left.equalTo(_avatarImageView.mas_right).with.offset(4);
make.right.equalTo(self.contentView).with.offset(-4);
make.bottom.equalTo(self.contentView).with.offset(-4);}
];
// 設(shè)置高度的Content Hugging
[_contentLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
2.UITableView
再看看UITableView。
用systemLayoutSizeFittingSize:獲取Cell的高度
在設(shè)定好Cell的約束以后哭靖,就可以用systemLayoutSizeFittingSize:方法獲取Cell的實際高度,它的參數(shù)可以設(shè)定為兩個系統(tǒng)常量侈离,如下:
UILayoutFittingCompressedSize: 返回合適的最小大小试幽。
UILayoutFittingExpandedSize: 返回合適的最大大小。
為了在“- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath ”方法中計算Cell的高度霍狰,我們需要一個專門用于計算高度的Cell實例抡草,可以說算是Cell的“模板”。一般來說蔗坯,這個實例可以設(shè)置成函數(shù)的static變量康震,并只在第一次使用時初始化一次。
model簡單緩存高度
為了避免每次滑動時計算高度宾濒,可以將Cell的高度緩存下來腿短。如,保存在每一行對應的數(shù)據(jù)Model(Entity)中,例如:
@interface Entity : NSObject
// Data
@property (copy, nonatomic) NSString *title;
// ...// Cache height
@property (assign, nonatomic) CGFloat cellHeight;
@end
tableView代理方法中代碼:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static Case4Cell *templateCell; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ templateCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Case4Cell class])]; });
// 獲取對應的數(shù)據(jù)
Case4DataEntity *dataEntity = _data[(NSUInteger) indexPath.row];
// 填充數(shù)據(jù)
[templateCell setupData:dataEntity];
// 判斷高度是否已經(jīng)計算過
if (dataEntity.cellHeight <= 0) {
// 根據(jù)當前數(shù)據(jù)橘忱,計算Cell的高度赴魁,
// ------ 注意+1 ------
dataEntity.cellHeight = [templateCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
}
return dataEntity.cellHeight;
}
改進:
iOS 8的新特性
iOS 8大大簡化了Cell的高度計算,只要:
1.設(shè)置好Cell中控件的約束;
- 然后 UITabelView初始化時候:
// UITabelView初始化時候
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 80;
3.在UITableview 代理方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 只用返回這個钝诚!
return UITableViewAutomaticDimension;
}