iOS 有关Cell高度自适应的几种方法(纯代码)

脑容量只能装这么多.jpeg

前言

作为一枚程序媛妹子,在这条撸码的道路上越走越远,表问:我怎么变这样~变得这样疯狂-_-!(不自觉哼起了tfboys的歌,sorry我是狂热阿姨粉= = )。
开发这段时间,一直没怎么写过博文,没好好的去总结零碎的知识点(那些零碎的知识点依旧零碎着= = )so~咳咳,为了强大自我的撸码水平提高逼格,以此作为开篇文章,陆续在空闲时间整理出常用的实用的管用的代码来巩固/强化/熟练必要的知识点。
so~ boys and girls 跟着我左手右手一个慢动作 嘿!

正题

言归正传,有关UITableViewCell高度自适应的这个问题,我想...对于绝大多数的你们早已不是问题,算是给刚入坑的小白们整理一下喽!纯代码方法,拖拽的别绕道,也过来瞅一瞅哈!

请忽略我偷来的评论.png

方法

  • 1.普通(或提前)计算cell的自适应高度
  • 2.通过FrameModel方式获取cell的自适应高度
  • 3.通过Masonry获取cell的自适应高度

注意:
大前提都是不要忘记在cell.m文件中,给contentLabel添加如下两行代码:

contentLabel.numberOfLines = 0;
[contentLabel sizeToFit];

1.普通(或提前)计算cell的自适应高度

我觉得不必说啥吧,算是入门级的简单方法了。
...
...
...
还是说一嘴...

普通计算:
1.在cell.m文件添加:

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.contentLabel sizeToFit];
}

注意:这里的 [self.contentLabel sizeToFit]要写在- (void)layoutSubviews方法里。

2.在viewcontroller.m文件中,为方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath中添加如下代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OneModel *model = self.array[indexPath.row];
    NSString *content = model.content;
    
    // 写法1.
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
    
    CGRect contentRect = [content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];

    // 写法2.
    //    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:content];
    //
    //    NSRange allRange = [content rangeOfString:content];
    //
    //    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:allRange];
    //
    //    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:allRange];
    //
    //    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    //    CGRect contentRect = [attrStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, CGFLOAT_MAX) options:options context:nil];
    
    CGFloat contentHeight;
    contentHeight = ceilf(contentRect.size.height);
    
    return  10 + 10 + 10 + contentHeight + 10;// timeHeight + contentHeight
}

搞定~!就是这么的easy哈哈哈哈哈

提前计算:
在普通计算的前提上进化的~
1.在cell.h文件中:

/** 
 根据指定的model数据返回当前cell的高度
 @param model 数据模型
 return cell高度
 */
+ (CGFloat)cellHeightWithModel:(OneModel *)model;

2.在cell.m文件中:

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.contentLabel sizeToFit];
}
+ (CGFloat)cellHeightWithModel:(OneModel *)model {
    
    NSString *content = model.content;
    
    // 写法1.
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
    CGRect contentRect = [content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    
    // 写法2.
//    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:content];
//    NSRange allRange = [content rangeOfString:content];
//    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:allRange];
//    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:allRange];
//    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
//    CGRect contentRect = [attrStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, CGFLOAT_MAX) options:options context:nil];
    
    CGFloat contentHeight;
    contentHeight = ceilf(contentRect.size.height);
    
    return  10 + 10 + 10 + contentHeight + 10;// timeHeight + contentHeight
}

3.在viewController.m文件中,添加私有方法,调用cell创建的+号方法,提前计算出不同的model内容对应的cell高度,然后依次添加到一个数组中:

#pragma mark - private methods
- (void)loadData {
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DataDemo" ofType:@"plist"];
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    for (NSDictionary *tempDic in data) {
        OneModel *model = [OneModel jxt_initWithDictionary:tempDic];
        [self.array addObject:model];
        
        CGFloat cellHeight = [One2Cell cellHeightWithModel:model];
        [self.heightArray addObject:[NSNumber numberWithFloat:cellHeight]];
    }
    [self.tableView reloadData];
}

在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法中,直接调用数组中对应的高度即可:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSNumber *cellHeight = self.heightArray[indexPath.row];
    return cellHeight.floatValue;
}

搞定~!

2.通过FrameModel方式获取cell的自适应高度

此方法需要创建一个FrameModel,用来计算cell的自适应高度。

1.创建FrameModel,继承与NSObject:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "TwoModel.h"

@interface TwoFrameModel : NSObject

@property (nonatomic, assign) CGFloat cellHeight;

@property (nonatomic, assign) CGRect timeLabelFrame;
@property (nonatomic, assign) CGRect contentLabelFrame;

@property (nonatomic, strong) TwoModel *model;

@end
  1. FrameModel.m中,计算每个frame值,最后计算cell高度:
- (void)setModel:(TwoModel *)model {
    
    _model = model;
    
    CGFloat timeLabelX = kPadding;
    CGFloat timeLabelY = kPadding;
    CGFloat timeLabelW = kWidth - 20;
    CGFloat timeLabelH = kPadding;
    
    self.timeLabelFrame = CGRectMake(timeLabelX, timeLabelY, timeLabelW, timeLabelH);
    
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:Kfont]};
    
    CGRect contentLabelRect = [_model.content boundingRectWithSize:CGSizeMake(kWidth - 20, 100000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    
    CGFloat contentLabelX = kPadding;
    CGFloat contentLabelY = CGRectGetMaxY(self.timeLabelFrame) + kPadding;
    CGFloat contentLabelW = contentLabelRect.size.width;
    CGFloat contentLabelH = contentLabelRect.size.height;
    
    self.contentLabelFrame = CGRectMake(contentLabelX, contentLabelY, contentLabelW, contentLabelH);

    
    // cellHeight
    self.cellHeight = CGRectGetMaxY(self.contentLabelFrame) + kPadding;
}

3.cell.h文件中:

@property (nonatomic, strong) TwoFrameModel *frameModel;

4.cell.m文件中:

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.timeLabel.frame = _frameModel.timeLabelFrame;
    self.contentLabel.frame = _frameModel.contentLabelFrame;
}

- (void)setFrameModel:(TwoFrameModel *)frameModel {
    
    _frameModel = frameModel;
    
    self.timeLabel.text = _frameModel.model.time;
    self.contentLabel.text = _frameModel.model.content;
}

5.在viewcontroller.m文件中

#pragma mark - private methods
- (void)loadData {
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DataDemo" ofType:@"plist"];
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    NSLog(@"data:%@", data);
    for (NSDictionary *tempDic in data) {
        TwoFrameModel *frameModel = [[TwoFrameModel alloc] init];
        frameModel.model = [TwoModel jxt_initWithDictionary:tempDic];
        [self.array addObject:frameModel];
    }
    
    [self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [self.array[indexPath.row] cellHeight];
}

搞定~!

3.通过Masonry获取cell的自适应高度

pod Masonry

1.创建tableview时,必须给一个估计高度:

_tableView.estimatedRowHeight = 175;// 需要给cell高度估计值,可以随便给,但不能不给

2.不需要写- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法,写了的话cell高度以此代理方法为准了= = 。

3.cell.m文件中,一定要给最后一个控件添加bottom距contentview的bottom间距!!!

#pragma mark - private methods
- (void)setUp {
    
    [self.contentView addSubview:[self timeLabel]];
    [self.contentView addSubview:[self contentLabel]];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kPadding);
        make.left.mas_equalTo(kPadding);
        make.right.mas_equalTo(-kPadding);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(kPadding);
        make.left.mas_equalTo(kPadding);
        make.right.mas_equalTo(-kPadding);
        make.bottom.mas_equalTo(-kPadding);// 最后一个控件的bottom是关键点!!!
    }];
    
}

OVER~~~~

怎么样 就是这么的简单粗暴!
可能我的方法有问题或者不够巧妙,有更好的方法或是我代码有不规范的地方,热烈欢迎大大们前来指教!!!

第一篇正式的博客就写到这里啦- - (日常追剧去-_-hahaha)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容