直接上代码:

1:先自定义cell

.h文件中

#import <UIKit/UIKit.h>
#import "LBDNewMsgListModel.h"
#import "MLEmojiLabel.h"@interface LBDMsgCenterCell : UITableViewCell<MLEmojiLabelDelegate, UIGestureRecognizerDelegate>@property (nonatomic, strong) UILabel *msgDateLbl;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIImageView *msgImgView;
@property (nonatomic, strong) UILabel *titleLbl;
@property (nonatomic, strong) UILabel *orderNum;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *lookLbl;
@property (nonatomic, strong) MLEmojiLabel *contentLbl;@property (nonatomic, strong) LBDNewMsgListModel *model;@end

.m文件中

#import "LBDMsgCenterCell.h"
#import "LBDNewMsgListModel.h"
#import "NSDate+Extend.h"@implementation LBDMsgCenterCell- (void)awakeFromNib {[super awakeFromNib];// Initialization code
}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {[self createView];[self setSubviews];}return self;
}#pragma make 创建子控件- (void)createView
{_msgDateLbl = [[UILabel alloc] init];_msgDateLbl.font = [UIFont systemFontOfSize:10];_msgDateLbl.textColor = [UIColor colorWithHex:@"646464"];_msgDateLbl.textAlignment = NSTextAlignmentCenter;[self.contentView addSubview:_msgDateLbl];_bgView = [[UIView alloc] init];_bgView.backgroundColor = [UIColor whiteColor];_bgView.layer.masksToBounds = YES;_bgView.layer.cornerRadius = 10;[self.contentView addSubview:_bgView];_msgImgView = [[UIImageView alloc] init];_msgImgView.hidden = YES;_msgImgView.contentMode = UIViewContentModeScaleToFill;[_bgView addSubview:_msgImgView];_topLineView = [[UIView alloc] init];_topLineView.hidden = YES;[_bgView addSubview:_topLineView];_titleLbl = [[UILabel alloc] init];_titleLbl.font = [UIFont systemFontOfSize:15];_titleLbl.numberOfLines = 0;_titleLbl.textColor = [UIColor colorWithHex:@"505050"];[_bgView addSubview:_titleLbl];_lookLbl = [[UILabel alloc] init];_lookLbl.textAlignment = NSTextAlignmentRight;;_lookLbl.text = @"立即查看";_lookLbl.font = [UIFont systemFontOfSize:13];[_bgView addSubview:_lookLbl];_lineView = [[UIView alloc] init];_lineView.backgroundColor = [UIColor unclickableColor];[_bgView addSubview:_lineView];[_bgView addSubview:self.contentLbl];
}- (void)setModel:(LBDNewMsgListModel *)model
{NSDateFormatter *formater = [[NSDateFormatter alloc] init];formater.dateFormat = @"yyyy/MM/dd HH:mm";NSDate *sendDate  = [formater dateFromString:model.createdAt];if ([sendDate isToday]) {_msgDateLbl.text = [NSString stringWithFormat:@"今天 %@", [model.createdAt timestampTransformDateToType:@"HH:mm"]];} else if ([sendDate isYesToday]) {_msgDateLbl.text = [NSString stringWithFormat:@"昨天 %@", [model.createdAt timestampTransformDateToType:@"HH:mm"]];} else {_msgDateLbl.text = [model.createdAt timestampTransformDateToType:@"yyyy/MM/dd HH:mm"];}_titleLbl.text = model.title;self.contentLbl.text = model.content;[_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {make.top.mas_equalTo(_msgDateLbl.mas_bottom);make.left.mas_equalTo(self.contentView.mas_left).offset(10);make.right.mas_equalTo(self.contentView.mas_right).offset(-10);make.bottom.mas_equalTo(self.contentView.mas_bottom);}];if ([model.colorType isEqualToString:@"1"]) {_topLineView.hidden = NO;_msgImgView.hidden = YES;[_msgImgView sd_setImageWithURL:nil];_topLineView.backgroundColor = [UIColor redColor];[_topLineView mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left);make.top.mas_equalTo(_bgView.mas_top);make.right.mas_equalTo(_bgView.mas_right);make.height.mas_equalTo(5);}];[_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left).offset(10);make.right.mas_equalTo(_bgView.mas_right).offset(-10);make.top.mas_equalTo(_topLineView.mas_bottom).offset(6);}];[self.contentLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_titleLbl.mas_left);make.right.mas_equalTo(_titleLbl.mas_right);make.top.mas_equalTo(_titleLbl.mas_bottom).offset(8);}];} else if ([model.colorType isEqualToString:@"2"]) {_topLineView.hidden = NO;_msgImgView.hidden = YES;[_msgImgView sd_setImageWithURL:nil];_topLineView.backgroundColor = [UIColor colorWithHex:@"00ade5"];[_topLineView mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left);make.top.mas_equalTo(_bgView.mas_top);make.right.mas_equalTo(_bgView.mas_right);make.height.mas_equalTo(5);}];[_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left).offset(10);make.right.mas_equalTo(_bgView.mas_right).offset(-10);make.top.mas_equalTo(_topLineView.mas_bottom).offset(6);}];[self.contentLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_titleLbl.mas_left);make.right.mas_equalTo(_titleLbl.mas_right);make.top.mas_equalTo(_titleLbl.mas_bottom).offset(8);}];} else if ([model.colorType isEqualToString:@"3"]) {_topLineView.hidden = YES;_msgImgView.hidden = NO;[_topLineView mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left);make.top.mas_equalTo(_msgDateLbl.mas_bottom);make.right.mas_equalTo(_bgView.mas_right);make.height.mas_equalTo(0);}];[_msgImgView sd_setImageWithURL:[NSURL URLWithString:model.activityImg] placeholderImage:[UIImage imageNamed:@"PersonalInformationImagePlaceholder"]];[_msgImgView mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left);make.right.mas_equalTo(_bgView.mas_right);make.top.mas_equalTo(_msgDateLbl.mas_bottom);make.height.mas_equalTo(SCREEN_WIDTH / 3.41);}];[_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_bgView.mas_left).offset(10);make.right.mas_equalTo(_bgView.mas_right).offset(-10);make.top.mas_equalTo(_msgImgView.mas_bottom).offset(6);}];[_contentLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(_titleLbl.mas_left);make.right.mas_equalTo(_titleLbl.mas_right);make.top.mas_equalTo(_titleLbl.mas_bottom).offset(8);}];}if ([model.jump isEqualToString:@"0"]) {[_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(self.contentLbl.mas_left);make.right.mas_equalTo(self.contentLbl.mas_right);make.height.mas_equalTo(0);make.top.mas_equalTo(self.contentLbl.mas_bottom);}];[_lookLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.right.mas_equalTo(self.contentLbl.mas_right);make.width.mas_equalTo(80);make.height.mas_equalTo(0);make.top.mas_equalTo(_lineView.mas_bottom);make.bottom.mas_equalTo(_bgView.mas_bottom);}];} else if ([model.jump isEqualToString:@"1"]) {[_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(self.contentLbl.mas_left);make.right.mas_equalTo(self.contentLbl.mas_right);make.height.mas_equalTo(0.5);make.top.mas_equalTo(self.contentLbl.mas_bottom);}];[_lookLbl mas_remakeConstraints:^(MASConstraintMaker *make) {make.right.mas_equalTo(self.contentLbl.mas_right);make.width.mas_equalTo(100);make.height.mas_equalTo(30);make.top.mas_equalTo(_lineView.mas_bottom);make.bottom.mas_equalTo(_bgView.mas_bottom);}];}}- (void)setSubviews
{[_msgDateLbl mas_makeConstraints:^(MASConstraintMaker *make) {make.left.mas_equalTo(self.contentView.mas_left);make.top.mas_equalTo(self.contentView.mas_top);make.right.mas_equalTo(self.contentView.mas_right);make.height.mas_equalTo(30);}];
}#pragma mark - delegate- (void)mlEmojiLabel:(MLEmojiLabel*)emojiLabel didSelectLink:(NSString*)link withType:(MLEmojiLabelLinkType)type
{switch(type){case MLEmojiLabelLinkTypeURL:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]];break;case MLEmojiLabelLinkTypePhoneNumber:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", link]]];break;case MLEmojiLabelLinkTypeEmail:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@", link]]];break;case MLEmojiLabelLinkTypeAt:NSLog(@"点击了用户%@",link);break;case MLEmojiLabelLinkTypePoundSign:NSLog(@"点击了话题%@",link);break;default:NSLog(@"点击了不知道啥%@",link);break;}}- (MLEmojiLabel *)contentLbl
{if (!_contentLbl) {_contentLbl = [MLEmojiLabel new];_contentLbl.numberOfLines = 0;_contentLbl.font = [UIFont systemFontOfSize:13.0f];_contentLbl.delegate = self;_contentLbl.backgroundColor = [UIColor whiteColor];_contentLbl.lineBreakMode = NSLineBreakByTruncatingTail;_contentLbl.textColor = [UIColor colorWithHex:@"a0a0a0"];_contentLbl.disableEmoji = NO;_contentLbl.lineSpacing = 3.0f;_contentLbl.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;}return _contentLbl;
}

2:在控制器.m文件中使用

- (void)drawView
{_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];_tableView.backgroundColor = [UIColor bgColor];_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;_tableView.delegate = self;_tableView.dataSource = self;[self addSubview:_tableView];// 给一个标识符,告诉tableView要创建哪个类[_tableView registerClass:[LBDMsgCenterCell class] forCellReuseIdentifier:@"LBDMsgCenterCell"];}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return [self.tableView fd_heightForCellWithIdentifier:@"LBDMsgCenterCell" cacheByIndexPath:indexPath configuration:^(LBDMsgCenterCell *cell) {// 在这个block中,重新cell配置数据源
        [self setupModelOfCell:cell atIndexPath:indexPath];}];
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return _msgArr.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{LBDMsgCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LBDMsgCenterCell"];cell.selectionStyle = UITableViewCellSelectionStyleNone;cell.backgroundColor = [UIColor clearColor];[self setupModelOfCell:cell atIndexPath:indexPath];return cell;
}- (void)setupModelOfCell:(LBDMsgCenterCell *) cell atIndexPath:(NSIndexPath *) indexPath {// 采用计算frame模式还是自动布局模式,默认为NO,自动布局模式//    cell.fd_enforceFrameLayout = NO;cell.model = _msgArr[indexPath.row];
}

转载于:https://www.cnblogs.com/xuzb/p/8706737.html

使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)...相关推荐

  1. ios开源框架——UITableView+FDTemplateLayoutCell优化UITableViewCell高度计算

    前言 这篇文章是我和我们团队最近对UITableViewCell利用AutoLayout自动高度计算和UITableView滑动优化的一个总结.从这篇文章里,你可以读到: UITableView高度计 ...

  2. iOS8+ UITableView自动计算cell高度并缓存

    这篇文章我们来讲一下UITableView的cell自适应高度,以及遇到的问题的解决办法.在看文章之前希望你已经会UITableView的基本使用了. 先奉上这篇文章的demo的Github地址:UI ...

  3. iOS UITableView+FDTemplateLayoutCell 配合AutoLayout分分钟教你实现类似微信朋友圈的动态高度自适应

    11.30日更新,实现了简单的微信朋友圈,点赞,评论,图片,高度自适应,下拉展开等各种效果Demo 点击打开链接 11.10更新 这种高度自适应的Label切记一定要加上这个属性 preferredM ...

  4. 开源库UITableView+FDTemplateLayoutCell学习

    摘自:优化UITableViewCell高度计算Swift版.优化UITableViewCell高度计算的那些事 本文带大家详细探索那篇文章所对应的库(1.2版),这个库就是利用缓存tableview ...

  5. 动态改变UITableView中的Cell高度

    往往在开发iPhone的应用过程中用得最多的应该算是UITableVIew了,凭着IOS给UITableView赋予了这种灵活的框架结构,让它不管在显示列表方面还是在排版方面都有着一定的优势.虽然UI ...

  6. IOS之Masonry约束的使用

    IOS之Masonry约束的使用 Masonry是做约束的,类似苹果开发的约束,做屏幕的适配,有xib开发,纯代码开发,storyboard约束等其他第三方框架. pod 加入 pod 'Masonr ...

  7. Masonry约束自定义TableViewCell自适应行高的约束冲突的问题

    问题@iOS-zhouyu类似 iOS Masonry约束自定义TableViewCell自适应行高的约束冲突的问题_上进求知,认真思辨的博客-CSDN博客_masonry tableviewcell ...

  8. UITableviewCell 使用Masonry撑开cell高度 遇见[LayoutConstraints] Unable to simultaneously satisfy constraints

    1. 问题描述 在布局UITableviewCell 内容时, 可用使用Masonry方便的自动计算高度撑开布局,但是当遇到cell高度不同,多个复杂的子view竖向排列时,容易产生高度计算冲突问题导 ...

  9. IOS 6.0+ Autolayout — UITableViewCell 高度调整

    要实现的效果 要求: storyboard上的准备工作 建好cell自定义AutoCell 类,关联好控件,事先准备好一些数据源 实现tableview的委托方法 核心部分,HeightForRow方 ...

最新文章

  1. 循序渐进学.Net Core Web Api开发系列【14】:异常处理
  2. 基于 HTML5 WebGL 的 3D 棉花加工监控系统
  3. Spring配置Bean的方式
  4. 个人开发者 android内购,【开发者账号】关于内购,协议税务的一些坑
  5. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)
  6. 文本编辑器查看 cprintf颜色_airWriter for mac(文本编辑软件)
  7. 浅谈SQL注入风险 - 一个Login拿下Server(转)
  8. 单向队列、双端队列、栈的模型实现
  9. server2012 asp odb数据原_异地服务器文件及数据库定时备份
  10. 信息——人类现代文明的奇迹
  11. Dos - 学习总结(1)
  12. PAIP.提升安全性----COOKIE绑定IP与城市与运营商
  13. firemonkey mysql_FireMonkey开发技术简明手册.pdf
  14. 获取windows用户密码——Jhon
  15. 7-4 求奇数和 (15 分)
  16. 一款令人发指的Linux监控软件
  17. RSA中的中国剩余定理(CRT)和多素数(multi-prime)
  18. ABD-Net: AttentivebutDiversePersonRe-Identification论文阅读
  19. 爱 —— 哥林多前书(1 Corinthians)第13章
  20. 常用激活函数:Sigmoid、Tanh、Relu、Leaky Relu、ELU、Mish、Swish等优缺点总结

热门文章

  1. Android Studio在线安装Android SDK注意事项
  2. 产品经理十八章:产品创新能力(二)
  3. Unity网格合并_材质合并
  4. 医疗卫生信息化 医学信息 医院管理 医疗信息化 资源下载
  5. 一名新晋程序员的自述:我的编程自学之路
  6. linux日志使用方法,Linux 日志终极指南
  7. 整合Spring Security
  8. MySQL高级 - 案例 - 系统性能优化分析
  9. ZkServer服务启动的逻辑-QuorumPeer.start
  10. 初步认识Volatile-CPU层面的内存屏障