使用第三方《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控件可能 ...
随机推荐
- Web开发HTTP中URI和URL的情感纠葛
作者:郭无心链接:https://www.zhihu.com/question/21950864/answer/66779836来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- Redis的Set数据类型
Sets 就是一个集合,集合的概念就是一堆不重复值的组合.利用Redis提供的Sets数据结构,可以存储一些集合性的数据,比如在微博应用中,可以将一个用户所有的关注人存在一个集合中,将其所有粉丝存在一 ...
- Django之(URL)路由系统
路由系统 简而言之,django的路由系统作用就是使views里面处理数据的函数与请求的url建立映射关系.使请求到来之后,根据urls.py里的关系条目,去查找到与请求对应的处理方法,从而返回给客户 ...
- linux下(ubuntu)反删除(误删恢复)与回收站制作
刚刚有个小伙伴不小心删了他写了好几的天代码,为他心疼之余帮他找回了文件. 想到我之前也常常误删一些文件,就干脆分享一下我的反删除方法,并说说我做的回收站(好low的,求大神指点) 首先是反删除软件ex ...
- WinSock 重叠IO模型
title: WinSock 重叠IO模型 tags: [WinSock 模型, 网络编程, 重叠IO模型] date: 2018-06-29 20:26:13 categories: Windows ...
- nodejs进阶(7)—async异步流程控制
Async介绍 Async是一个流程控制工具包,提供了直接而强大的异步功能.基于Javascript为Node.js设计,同时也可以直接在浏览器中使用. Async提供了大约20个函数,包括常用的 m ...
- sql server 数据库代码备份及还原代码
--备份 BACKUP DATABASE [库名称] TO DISK='E:\qq\ddd.bak' --备份并覆盖 BACKUP DATABASE [库名称] TO DISK='E:\qq\ddd. ...
- linux 中环境变量配置文件说明
1. 修改/etc/profile文件 特点:所有用户的shell都有权使用你配置好的环境变量 说明:如果你的电脑仅用作开发,建议使用此配置,因为所有用户的shell都有权使用你配置好的环境变量,所以 ...
- Android学习——Fragment动态加载
动态加载原理 利用FragmentManager来添加一套Fragment事务,最后通过commit提交该事务来执行对Fragment的相关操作. FragmentManager fragmentma ...
- ubuntu怎么关防火墙
1.关闭ubuntu的防火墙 ufw disable2.卸载了iptables apt-get remove iptables 1.用iptables -F这个命令来关闭防火墙,但是使用这个命令前,千 ...