使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)
直接上代码:
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约束)的更多相关文章
- AutoLayout 根据文字、图片自动计算 UITableViewCell 高度
原文网址: http://lvwenhan.com/ios/449.html 此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考 ...
- 使用第三方UITableView+FDTemplateLayoutCell计算cell行高注意点
现在很方便的计算单元格的行高大部分都是使用的第三方框架UITableView+FDTemplateLayoutCell,不知道你在使用这个框架的时候有没有遇到和我一样的问题,比如: 在这样计算cell ...
- 自动计算UITableViewCell高度2(CGRect约束)
1.先创建model .h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface LBDNe ...
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- 《转》优化UITableViewCell高度计算的那些事
我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...
- UITableView自动计算cell高度并缓存
原文链接:http://www.jianshu.com/p/64f0e1557562 cell高度计算的历史 在iOS8之前,如果UITableViewCell的高度是动态的,如果想要显示正确的话,我 ...
- Masonry与UITableView+FDTemplateLayoutCell搭配使用
打个小广告:本人开发了一个宠物相关的App,欢迎大家下载体验~ 下载二维码: 进入正文: 之前发过一篇博客,也是对这两个的练习使用,但是之后遇到些问题,所以删除重写了.抱歉 Masonry是一款轻量级 ...
- 优化UITableViewCell高度计算的那些事(RunLoop)
这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...
- UITableViewCell 高度计算从混沌初始到天地交泰
[原创]UITableViewCell 高度计算从混沌初始到天地交泰 本文主要基予iOS UITableViewCell 高度自适应计算问题展开陈述,废话少说直入正题: UITableView控件可能 ...
随机推荐
- 在 Linux 上创建第一个 Service Fabric Java 应用程序
先决条件 开始之前,请安装 Service Fabric SDK.Azure CLI,并在 Linux 开发环境中设置开发群集. 如果使用 Mac OS X,则可使用 Vagrant 在虚拟机中设置 ...
- web中的HTTP协议
HTTP协议(HyperText Transfer Protocol 超文本传输协议),是浏览器接收web页面和发送web页面请求的标准协议.HTTP协议是基于TCP/IP协议的,版本号有Http1. ...
- TCP/IP五层模型详解
将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议就理解了整个互联网通信的原理. 首先,用户感知到的只是最上面一层应用层,自上而下每层都依赖 ...
- php *-devel
源码编译安装个php,缺少好多-devel的库. why devel? devel包至少包括头文件和链接库.如果你的要安装的源码依赖某个库,那肯定需要这两样东西. 让apache支持php 在编译ph ...
- 多线程(三)~多线程中数据的可见性-volatile关键字
我们先来看一段代码: ①.线程类,用全局布尔值控制线程是否结束,每隔1s打印一次当前线程的信息 package com.multiThread.thread; publicclassPrintStri ...
- spring----spring中的注解@service等的作用
service 是有用的相当于 xml配置中得bean id = service 也可以不指定 不指定相当于 bean id = com. service.service 就是这个类的全限定名 ...
- 一个sql server 实施工程师的反思
自14年开始从事数据库实施,至今(2018-02-16)晃眼间已经四个年头过去了,工作上的能力要求多数能自己解决,可这不应该成为我学习路上的终点.加之总觉得自己对sql 的理解有些浮于表面,所以借着春 ...
- ORA-01795: 列表中的最大表达式数为1000的解决方法
IN中的数据量不能超过1000条. 解决方案:把条件分成多个少于1000的IN即: DELETEFROMT_MM_SECTION_SITE_UPDATEWHERE T.T_MM_SECTION_SL_ ...
- Oracle日志组添加冗余文件和日志组
rac中需要指定thread添加日志组RAC:alter database add logfile thread 1 group 1('+DATA/irac/redo01_1.log','+DATA/ ...
- 我的HTML总结之常用基础便签
HTML:是Hyper Text Markup Language(超级文本标记语言)的缩写,HTML不是一种程序,只是一种控制网页中数据显示的标识语言. HTML由一组标签组成. HTML的基本结构 ...