之前我們已經對Cell高度自適應進行了幾次研究:
UITableViewCell高度自適應探索--UITableView+FDTemplateLayoutCell
地址: http://www.lxweimin.com/p/7839e3a273a6
UITableViewCell高度自適應探索--cell預估高度(一)
地址: http://www.lxweimin.com/p/6ab92579fcf1
UITableViewCell高度自適應探索--cell預估高度(二)
地址: http://www.lxweimin.com/p/f3609cd9392e
今天,再提供一種AutoLayout與Frame相結合的方式來設置cell高度的方法.
今天這個方法的要點是:
- 使用Autolayout在進行布局.
- 使用Frame進行高度計算
- 使用模型的屬性緩存每個Cell第一次計算的高度.
相對于之前說的那些方法,這個方法比UITableView+FDTemplateLayoutCell使用起來更簡單和容易理解(自從寫FD那篇文章發表后收到很多網友的關于使用的問題,大部分是由于沒有使用正確);并且克服了預估高度方式的那些問題,也不用把約束改來改去, 使計算的過程更加可控.
這種方法雖然是使用fram的方式計算,但是如果沒有autoLayout,計算的過程就會復雜幾倍,有時候可能還需要一個專門的類去管理子控件的frame.在我看來是一個比較不錯的方法.
進入正題.
先看要實現的效果:
其中文字的長度不一,圖片可能有或沒有.為了排除其他干擾,數據來自plist文件.
- 這是我們自定義cell的設置,這些元素是固定的,我們使用AutoLayout對它們幾個進行布局.
- 創建一個Message模型,賦予其對應的屬性.
由于cell的高度本質上還是基于模型數據來算的,所以計算高度的任務交給模型,故模型同時開放一個cellHeight的只讀屬性,將來好拿給控制器使用.
@interface Message : NSObject
// 頭像、名字、和描述文字我給固定了,所以沒有弄屬性處理
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, assign, readonly) CGFloat cellHeight;
@end
- 模型計算Cell高度,通過重寫cellHeight的getter方法實現
- (CGFloat)cellHeight {
if (!_cellHeight) {
CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * margin; // 屏幕寬度減去左右間距
CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contentFont]}
context:nil].size.height;
_cellHeight = contentLabelY + contentH + margin;
}
return _cellHeight;
}
注意:
上面高度的計算還沒有將內容圖片的高度計算在內.
并且實現了利用模型的cellHeight屬性緩存第一次計算高度.
- 由于內容圖片不是每個cell都有,所以使用代碼動態添加.
// 屬性聲明
@property (strong, nonatomic) UIImageView *contentImageView;
// getter實現
- (UIImageView *)contentImageView {
if (!_contentImageView) {
_contentImageView = [[UIImageView alloc] init];
[self.contentView addSubview:_contentImageView];
}
return _contentImageView;
}
- cell該接收模型了
@property (nonatomic, strong) Message *message;
- (void)setMessage:(Message *)message {
_message = message;
self.contentLabel.text = _message.content;
if (message.imageName.length) {
self.contentImageView.hidden = NO;
self.contentImageView.image = [UIImage imageNamed:message.imageName];
}
else {
self.contentImageView.hidden = YES;
}
}
當然,這時候contentImageView當然是顯示不出來的,因為我們還沒有贈送它一個frame.那么它的frame從何而來呢?
一開始我們說過,計算要基于模型,所以接下來的思路是由模型算出imageView的frame,拿去給cell用.
回到模型cellHeight的getter方法,添加對imageName屬性的處理:
// 圖片將要展示的frame作為模型的其中一個屬性
@property (nonatomic, assign) CGRect contentImageFrame;
- (CGFloat)cellHeight {
if (!_cellHeight) {
CGFloat contentW = [UIScreen mainScreen].bounds.size.width - 2 * margin; // 屏幕寬度減去左右間距
CGFloat contentH = [self.content boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:contentFont]}
context:nil].size.height;
_cellHeight = contentLabelY + contentH + margin;
// 對imageName屬性的處理
if (self.imageName.length) {
UIImage *image = [UIImage imageNamed:self.imageName];
CGFloat imageH = image.size.height;
CGFloat imageW = image.size.width;
// 直接得出contentImageView應該顯示的frame
_contentImageFrame = CGRectMake(margin, _cellHeight, imageW, imageH);
_cellHeight += imageH + margin;
}
}
return _cellHeight;
}
當上面代碼執行完畢,contentImageFrame就有值了.接著,回到cell的setMessage:方法給contentImageView賦值.
- (void)setMessage:(Message *)message {
_message = message;
self.contentLabel.text = _message.content;
if (message.imageName.length) {
self.contentImageView.hidden = NO;
self.contentImageView.image = [UIImage imageNamed:message.imageName];
// 給圖片的frame賦值,這個值就是上面得到那個
self.contentImageView.frame = _message.contentImageFrame;
}
else {
self.contentImageView.hidden = YES;
}
}
- 這時候使用起來就非常輕松了
// 控制器tableView協議方法實現
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Message *message = self.dataList[indexPath.row];
return message.cellHeight;
}