在我的项目里面,我自定义了一个cell,然后用masonry进行约束,tableview的cell自适应label的高度

cell里面:

#import "MedicalRecordCell.h"
@interface MedicalRecordCell ()@property (weak, nonatomic) UILabel *fieldnameLabel;
@property (weak, nonatomic) UILabel *uomLabel;
@property (weak, nonatomic) UILabel *valueLabel;
@property (weak, nonatomic) UIView *lineView;
@endstatic CGFloat const margin = 10;
@implementation MedicalRecordCell- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {UILabel *fieldnameLabel = [UILabel label];fieldnameLabel.textColor = [UIColor darkGrayColor];fieldnameLabel.font = [UIFont boldSystemFontOfSize:17];fieldnameLabel.textAlignment = NSTextAlignmentLeft;fieldnameLabel.backgroundColor = RANDOMCOLOR(0.6);[self.contentView addSubview:fieldnameLabel];self.fieldnameLabel = fieldnameLabel;UILabel *uonLabel = [UILabel label];[self.contentView addSubview:uonLabel];self.uomLabel = uonLabel;UILabel *valueLabel = [UILabel label];valueLabel.backgroundColor = RANDOMCOLOR(0.6);valueLabel.textAlignment = NSTextAlignmentRight;valueLabel.numberOfLines = 0;valueLabel.font = [UIFont systemFontOfSize:15];[self.contentView addSubview:valueLabel];self.valueLabel = valueLabel;UIView *lineView = [UIView new];lineView.backgroundColor = CELLLINE_COLOR;[self.contentView addSubview:lineView];self.lineView = lineView;[self setViewAutoLayout];}return self;}- (void)setViewAutoLayout
{[self.fieldnameLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(self.contentView).offset(margin);make.top.mas_equalTo(self.contentView).offset(margin);}];[self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(self.fieldnameLabel.mas_right).offset(margin);make.right.mas_equalTo(self.uomLabel.mas_left).offset(-margin);make.top.mas_equalTo(self.contentView).offset(margin);make.bottom.mas_equalTo(self.contentView).offset(-margin);}];[self.uomLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.right.equalTo(self.contentView).offset(-margin);make.width.offset(40);make.height.offset(40);make.centerY.equalTo(self.contentView);}];[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.right.bottom.equalTo(self.contentView);make.height.offset(1);}];
}
- (void)setMedicalrecordSectionModel:(MedicalrecordSectionModel *)medicalrecordSectionModel
{_medicalrecordSectionModel = medicalrecordSectionModel;self.fieldnameLabel.text = [NSString stringWithFormat:@"%@ :",medicalrecordSectionModel.fieldname];self.uomLabel.text = medicalrecordSectionModel.uom;self.valueLabel.text = medicalrecordSectionModel.value;if ([medicalrecordSectionModel.metatype isEqualToString:@"radio"]) {NSArray *options = [medicalrecordSectionModel.metatextassemble componentsSeparatedByString:@"|"];NSInteger value = [medicalrecordSectionModel.value integerValue];if(value) self.valueLabel.text = options[value - 1];}}@end

控制器设置tableview自适应

 self.tableView.rowHeight = UITableViewAutomaticDimension;self.tableView.estimatedRowHeight = 44;

然而,不管是上面那样把约束写在初始化话方法里,还是我原先的把约束写在layoutSubviews里,都不能让内容正常的显示

你看看,你看看,真是可怜,tableview来回滚动的时候还偶尔会显示正常,反正就是看他心情来显示...

后来我解决的办法就是cell里面用模型赋值之后调用了一下layoutIfNeeded  没想到竟然就解决了这个问题

别人是这么说滴:

-layoutIfNeeded方法:如果,有需要刷新的标记,立即调用layoutSubviews进行布局(如果没有标记,不会调用layoutSubviews)
如果要立即刷新,要先调用[view setNeedsLayout],把标记设为需要布局,然后马上调用[view layoutIfNeeded],实现布局
在视图第一次显示之前,标记总是“需要刷新”的,可以直接调用[view layoutIfNeeded]

- (void)setMedicalrecordSectionModel:(MedicalrecordSectionModel *)medicalrecordSectionModel
{_medicalrecordSectionModel = medicalrecordSectionModel;self.fieldnameLabel.text = [NSString stringWithFormat:@"%@ :",medicalrecordSectionModel.fieldname];self.uomLabel.text = medicalrecordSectionModel.uom;self.valueLabel.text = medicalrecordSectionModel.value;if ([medicalrecordSectionModel.metatype isEqualToString:@"radio"]) {NSArray *options = [medicalrecordSectionModel.metatextassemble componentsSeparatedByString:@"|"];NSInteger value = [medicalrecordSectionModel.value integerValue];if(value) self.valueLabel.text = options[value - 1];}[self layoutIfNeeded];}

这样基本是可以正常显示了,但是cell的高度一直是以 valueLabel 底部约束. 那么,有个问题就出现了 ,如果 模型value没有值就会导致label没有高度,回事cell成一坨坨的哦,所以这里要给label一个默认值,让其有高度就可以了

转载于:https://www.cnblogs.com/yulongjiayuan/p/5704597.html

layoutIfNeeded 就这样把我害惨相关推荐

  1. mysql3d000啥意思_我是Redis,MySQL大哥被我害惨了!

    我是Redis 你好,我是Redis,一个叫Antirez的男人把我带到了这个世界上. 说起我的诞生,跟关系数据库MySQL还挺有渊源的. 在我还没来到这个世界上的时候,MySQL过的很辛苦,互联网发 ...

  2. 我是Redis,MySQL大哥被我害惨了!

    我是Redis 你好,我是Redis,一个叫Antirez的男人把我带到了这个世界上. 说起我的诞生,跟关系数据库MySQL还挺有渊源的. 在我还没来到这个世界上的时候,MySQL过的很辛苦,互联网发 ...

  3. 我是 Redis,MySQL 大哥被我害惨了!

    作者 | 轩辕之风O 来源 | 编程技术宇宙(ID:xuanyuancoding) 我是Redis 你好,我是Redis,一个叫Antirez的男人把我带到了这个世界上. 说起我的诞生,跟关系数据库M ...

  4. redis失效了mysql扛不住_我是Redis,MySQL大哥被我害惨了!

    我是Redis 你好,我是Redis,一个叫Antirez的男人把我带到了这个世界上. 说起我的诞生,跟关系数据库MySQL还挺有渊源的. 在我还没来到这个世界上的时候,MySQL过的很辛苦,互联网发 ...

  5. 为了这个技术,操作系统把 CPU 害惨了!

    来源 | 编程技术宇宙 责编 | Carol 封图 | CSDN 下载自视觉中国 内存访问瓶颈 我是CPU一号车间的阿Q,前一阵子我们厂里发生了一件大喜事,老板拉到了一笔投资,准备扩大生产规模. 不过 ...

  6. Spring Boot集成Redis,这个坑把我害惨了!

    最近项目中使用SpringBoot集成Redis,踩到了一个坑:从Redis中获取数据为null,但实际上Redis中是存在对应的数据的.是什么原因导致此坑的呢? 本文就带大家从SpringBoot集 ...

  7. 坑爹的 Lombok,把我害惨了!

    来源:juejin.im/post/6881432532332576781 序言 去年在项目当中引入了Lombok插件,着实解放了双手,代替了一些重复的简单工作(Getter,Setter,toStr ...

  8. 程序员吐槽:组里新来一个“加班狂”,可把大家害惨了

    前两天关于<华为阿里员工跳槽到微软后遭抵制>的话题引发了网友们对奋斗逼的热议! 你有遇到过这样的情况吗?最近又有网友在职场论坛吐槽:组里新来了一个特别能装,很能奋斗的人,但是都是在老板面前 ...

  9. 谁有全民一起mysql_我是Redis,MySQL大哥被我害惨了!

    我是Redis 你好,我是Redis,一个叫Antirez的男人把我带到了这个世界上. 说起我的诞生,跟关系数据库MySQL还挺有渊源的. 在我还没来到这个世界上的时候,MySQL过的很辛苦,互联网发 ...

  10. 那些害惨我们的伪科学(上)

    主播 / 朱峰.姝琦 嘉宾 / 粒粒.C哥 后期 / 朱峰 这期跟大家聊聊关于伪科学的事儿,提到伪科学,很多人的第一印象可能是那些不靠谱的封建迷信,但其实还有很多裹着科学外衣,看似有点道理的伪科学,这 ...

最新文章

  1. 清华「男神」沈天成,踢毽子踢成了2021学生年度人物
  2. css3 box-sizing:border-box 实现div一行多列
  3. response html 页面,使用response将html拼接页面写到当前浏览器端完成自动提交功能...
  4. vimrc for windows
  5. python字符串27种常见的方法
  6. smartform四联纸跳页问题
  7. Handle In-Day Changes
  8. MySQL模糊查询—like关键字
  9. [SDOI2015]约数个数和
  10. P4503-[CTSC2014]企鹅QQ【字符串hash】
  11. Ubuntu下如何正确安装FFmpeg
  12. 【渝粤题库】国家开放大学2021春2681煤矿采掘技术题目
  13. 【leetcode】ZigZag Conversion
  14. IE6下PNG图片透明效果(PNG图片做背景也可以)
  15. 《英雄联盟》手游官宣!仍是原来配方和味道 它还能再火十年吗?
  16. 解决h264bitstream的一个bug
  17. 大学计算机软件基础心得体会,学习计算机应用基础心得体会.doc
  18. 【操作系统】—进程的状态与转换
  19. @程序员,想要基于 Python 3.4 玩爬虫该看些什么?
  20. 【BZOJ3924】[Zjoi2015]幻想乡战略游戏 动态树分治

热门文章

  1. Git——Windows下Git的安装
  2. linux exec 脚本之家,详解Shell脚本中调用另一个Shell脚本的三种方式
  3. android代码删除wifi,Android Wifi的forget()操作实例详解_Android_脚本之家
  4. mysql可靠性优先策略
  5. ffmpeg命令解析
  6. 截至2006年3月1日全球CCIE人数统计
  7. Intellij Idea 创建maven WebAPP项目
  8. 前端工程师做事的三重境界:我的进阶之路
  9. Visual Studio 发布新版API智能提示
  10. windows server 2012 动态访问控制