一说明:
转载自http://blog.youkuaiyun.com/u011043997/article/details/51263016
二:四种方法计算cell的高度(推荐使用第三种)
1、iOS8的自动计算机制,需要autolayout(适用iOS8之后系统)
2、iOS6之后系统API结合autolayout进行计算(适用于iOS6之后的系统)
3、手动计算(适用于iOS6之后的系统)
4、借助于第三方框架自动计算(适用于iOS6之后的系统)
方法1:iOS8的自动计算
此方法必须使用autolayout,这里我是用的xib设置的,也可以使用第三方框架masonry设置。
设置约束的时候必须注意每个控件在垂直方向上必须都有约束,这样cell才可以计算出来高度。
下面我们来看看cell的内部控件的垂直方向的约束如何设置
昵称label的垂直约束
内容文字label的垂直约束
内容图片的垂直约束
height <= 400这个约束也可以不用设置,我这里是为了不让图片过长,所以限制了高度。

下面我只贴出计算高度的代码,整个Demo我会放大github上面。
具体实现代码
iOS8时代的高度计算非常简单,下面两行代码就搞定了,非常方便。
前提是需要设置好在垂直高度上的约束。
- - (void)viewDidLoad{
- self.tableView.estimatedRowHeight = 80.0f;
- self.tableView.rowHeight = UITableViewAutomaticDimension;
- }
效果如下
方法2:iOS6的系统API结合autolayout
控件的约束和第一个方法的一样,下面列出的代码是和第一个方法不同的地方。
该方法的demo和第一个方法的demo是同一个,每个方法独立使用到的代码我会特别注明,没有注明就是所有方法共有的。
-
-
- - (void)setModel:(TableViewModel *)model
- {
-
- CGFloat preferredWidth = [UIScreen mainScreen].bounds.size.width - 53;
- self.userName.preferredMaxLayoutWidth = preferredWidth;
- self.userContentString.preferredMaxLayoutWidth = preferredWidth;
-
- self.headImage.image = model.userHeadImage;
- self.userContentImage.image = model.userContentImage;
- self.userContentString.text = model.userContentString;
- self.userName.text = model.userName;
- }
-
-
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- static TableViewCell *Cell;
- static dispatch_once_t onceToken;
-
- dispatch_once(&onceToken, ^{
- Cell = [tableView dequeueReusableCellWithIdentifier:CellId];
- });
-
- TableViewModel *model = self.modelArray[indexPath.row];
- Cell.model = model;
-
- model.cellHeight = [Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height +1.0f;
- return model.cellHeight;
- }
-
-
- - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return 112.0f;
- }
该方法实现效果和方法一相同
方法3、手动计算
该方法需要手动计算垂直高度上每个控件的高度,然后相加得出cell的高度。
这种方法最繁琐,但是也是最精确的,也是最可控的。
使用这个方法,可以不需要使用autolayout设置约束,直接使用frame设置每个控件的位置。但是为了方便,我这里还是使用autolayout设置控件的约束和位置。
因为需要确切的知道每个控件的高度,所以这里image的高度必须是固定的,这样才可以进行cell的高度计算
修改如下
修改的代码如下:
-
-
- #import "TableViewModel.h"
-
- @implementation TableViewModel
-
-
- - (CGFloat)cellHeight{
-
- CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 53, MAXFLOAT);
-
-
- CGFloat textH = [self.userContentString boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
-
-
-
-
-
-
-
-
- _cellHeight = 0 + 17 + 8.5 + 8 +textH + 304;
-
- return _cellHeight;
- }
-
-
-
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- TableViewModel *model = self.modelArray[indexPath.row];
- return model.cellHeight;
- }
方法四、使用第三方框架
这是国内的一个大神写的框架,可以一行代码就实现cell的高度自动计算。同时还能实现缓存高度,最低兼容版本为iOS6。
实现代码就不写了,非常简单
具体看这篇文章:《优化UITableViewCell高度计算的那些事》
这里啰嗦两点,也是自己踩过的坑:
1、在博主的文字里面提到使用的时候直接使用如下代码即可:
- #import <UITableView+FDTemplateLayoutCell.h>
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return [tableView fd_heightForCellWithIdentifier:@"identifer" cacheByIndexPath:indexPath configuration:^(id cell) {
-
- cell.entity = self.feedEntities[indexPath.row];
- }];
- }
一定要把上面的id cell,换成自己的cell类,比如我的就是WSTableViewCell *cell。算是一个小坑吧。
2、很多人肯定吃过self-sizing-cell的亏,觉得我上面都设置对了,为什么就是算不出来高度呢?
要满足self-sizing-cell,必须满足两点:
- 你的cell里面的控件必须在上下左右四个方向都有约束到cell的四个边。如下图:

*约束一定要是控件和cell的contentView边缘之间的约束,而不是控件和cell边缘的之间的约束。
因为设计给的图,cell内部的控件和cell的距离是到cell边缘的距离,然后我就发现怎么都不能进行高度自动计算,所有的cell全部叠在一起了。被这个坑了很久,一直找不出来原因。另外cell的边缘和cell的contentView的边缘相差8pt。
错误设置:
ReplayCell是cell的名字
正确设置:
superView是cell的contentView
三:github地址