直接上代码:

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. SpringSecurity 3.2入门(9)自定义权限控制代码实现

    1. 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,我们的所有控制将在 ...

  2. js中的Function和Object

    说到构造器(condtructor).原型链(prototype),说道Function与Object,总要祭出下面这张图 1.Function是最顶层的构造器,Object是最顶层的对象 2.先有的 ...

  3. 机器学习kNN

    from numpy import * import operator def createDataSet(): group = array([[1.0, 1.1], [1.0, 1.0], [0, ...

  4. DOM 和 BOM

    DOM 和  BOM DOM: DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句话说,这是表示和处理一个H ...

  5. Android开发之EditText利用键盘跳转到下一个输入框

    以前做项目的时候,从来没考虑过这些.这段时间公司内部用的一款APP,就出现了这个问题,在登录或者注册的时候,点击键盘的回车按钮,可以跳到下一个输入框的功能,这个属性一直也没记住,今天就把自己一直没记过 ...

  6. MD5简单实例

    如图当点击按钮时,会先判断是否第一次登陆,如果是第一次登陆登陆则会弹出设置密码的弹窗,若果登陆过则弹出登陆弹窗 其中输入的密码会用MD5加密下 package com.org.demo.wangfen ...

  7. 杂谈spring、springMVC

    一.背景 目前项目组都在用SSM(spring+springMVC+mybatis)开发项目 大家基本都停留在框架的基本使用阶段,对框架的职责并不清晰,导致配置文件出现了不少问题 在这简单讲解一下sp ...

  8. (二)svn服务端安装配置

    两种服务端安装包 官方安装包 官方网站:http://subversion.apache.org/ 下载:http://subversion.apache.org/download.cgi 官方提供的 ...

  9. t d x 示例z

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...

  10. 再学UML-UML用例建模解析(一)

    UML(统一建模语言)是当前软件开发中使用最为广泛的建模技术之一,通过使用UML可以构造软件系统的需求模型(用例模型).静态模型.动态模型和架构模型.UML通过图形和文字符号来描述一个系统,它是绘制软 ...