直接上代码:

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:];
_msgDateLbl.textColor = [UIColor colorWithHex:@""];
_msgDateLbl.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:_msgDateLbl]; _bgView = [[UIView alloc] init];
_bgView.backgroundColor = [UIColor whiteColor];
_bgView.layer.masksToBounds = YES;
_bgView.layer.cornerRadius = ;
[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:];
_titleLbl.numberOfLines = ;
_titleLbl.textColor = [UIColor colorWithHex:@""];
[_bgView addSubview:_titleLbl]; _lookLbl = [[UILabel alloc] init];
_lookLbl.textAlignment = NSTextAlignmentRight;;
_lookLbl.text = @"立即查看";
_lookLbl.font = [UIFont systemFontOfSize:];
[_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();
make.right.mas_equalTo(self.contentView.mas_right).offset(-);
make.bottom.mas_equalTo(self.contentView.mas_bottom);
}]; if ([model.colorType isEqualToString:@""]) { _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();
}]; [_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left).offset();
make.right.mas_equalTo(_bgView.mas_right).offset(-);
make.top.mas_equalTo(_topLineView.mas_bottom).offset();
}]; [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();
}]; } else if ([model.colorType isEqualToString:@""]) { _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();
}]; [_titleLbl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(_bgView.mas_left).offset();
make.right.mas_equalTo(_bgView.mas_right).offset(-);
make.top.mas_equalTo(_topLineView.mas_bottom).offset();
}]; [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();
}]; } else if ([model.colorType isEqualToString:@""]) { _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();
}]; [_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();
make.right.mas_equalTo(_bgView.mas_right).offset(-);
make.top.mas_equalTo(_msgImgView.mas_bottom).offset();
}]; [_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();
}]; } if ([model.jump isEqualToString:@""]) { [_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();
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();
make.height.mas_equalTo();
make.top.mas_equalTo(_lineView.mas_bottom);
make.bottom.mas_equalTo(_bgView.mas_bottom);
}]; } else if ([model.jump isEqualToString:@""]) { [_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();
make.height.mas_equalTo();
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();
}];
} #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 = ;
_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];
}

使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)的更多相关文章

  1. AutoLayout 根据文字、图片自动计算 UITableViewCell 高度

    原文网址: http://lvwenhan.com/ios/449.html 此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考 ...

  2. 使用第三方UITableView+FDTemplateLayoutCell计算cell行高注意点

    现在很方便的计算单元格的行高大部分都是使用的第三方框架UITableView+FDTemplateLayoutCell,不知道你在使用这个框架的时候有没有遇到和我一样的问题,比如: 在这样计算cell ...

  3. 自动计算UITableViewCell高度2(CGRect约束)

    1.先创建model .h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface LBDNe ...

  4. 优化UITableViewCell高度计算的那些事

    优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...

  5. 《转》优化UITableViewCell高度计算的那些事

    我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...

  6. UITableView自动计算cell高度并缓存

    原文链接:http://www.jianshu.com/p/64f0e1557562 cell高度计算的历史 在iOS8之前,如果UITableViewCell的高度是动态的,如果想要显示正确的话,我 ...

  7. Masonry与UITableView+FDTemplateLayoutCell搭配使用

    打个小广告:本人开发了一个宠物相关的App,欢迎大家下载体验~ 下载二维码: 进入正文: 之前发过一篇博客,也是对这两个的练习使用,但是之后遇到些问题,所以删除重写了.抱歉 Masonry是一款轻量级 ...

  8. 优化UITableViewCell高度计算的那些事(RunLoop)

    这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...

  9. UITableViewCell 高度计算从混沌初始到天地交泰

    [原创]UITableViewCell 高度计算从混沌初始到天地交泰 本文主要基予iOS UITableViewCell 高度自适应计算问题展开陈述,废话少说直入正题: UITableView控件可能 ...

随机推荐

  1. jQuery获取table表中的td标签

    首先我来介绍一下我遇到的问题 1.当有一个table表包含了<tr>标签,<td>标签,大致可以认为是这样的: <tr> <td> @scene.ID ...

  2. Java 的版本历史与特性

    Java SE 8[2014-03-14发行] Lambda表达式 Pipelines和Streams Date和Time API Default方法 Type注解 Nashhorn JavaScri ...

  3. linux shell基础编程2

    while循环 语法1: while [ 条件 ] do 命令序列 done 语法2: while read -r line do 命令序列 done (切记while和左中括号一定要有空格) 例子 ...

  4. Java web service 异常

    1.org/apache/commons/discovery/tools/DiscoverSingleton Exception in thread "main" java.lan ...

  5. PAT 1021 Deepest Root

    #include <cstdio> #include <cstdlib> #include <vector> using namespace std; class ...

  6. HTML表格相关元素

    <table> 标签定义 HTML 表格.简单的 HTML 表格由 table 元素以及一个或多个 tr.th 或 td 元素组成.tr 元素定义表格行,th 元素定义表头,td 元素定义 ...

  7. cocoapods的安装和安装过程中遇到的问题

    查看当前的ruby版本,我的版本是ruby 2.0.0p648 小于2.2安装cocoapods时会遇到以下问题 $ ruby -v 查看当前ruby源,默认为 https://rubygems.or ...

  8. Java—集合框架List

    集合的概念 现实生活中:很多的事物凑在一起 数学中的集合:具有共同属性的事物的总和 Java中的集合类:是一种工具类,就像是容器,存储任意数量的具有共同属性的对象 集合的作用 在类的内部,对数据进行组 ...

  9. Linux下Kafka单机安装配置

    安装jdkJDK版本大于1.8 安装kafkatar -zxvf kafka_2.11-0.10.2.1.tgz mv kafka_2.11-0.10.2.1 /usr/local/kafka 配置k ...

  10. Jupyter notebook远程访问linux服务器

    [转]https://blog.csdn.net/akon_wang_hkbu/article/details/78973366