使用第三方《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控件可能 ...
随机推荐
- 程序员心灵鸡汤桌面壁纸1080p 欢迎大家下载,HTML,PHP,node,css,前端
上段时间在读书的时候看见了一句话:一张地图,不论比例多么精确,它永远不可能带着主人在地面上移动半步, 作为爱思考,爱学习的程序员 突然想到了:一份帮助文档,不论多么详细,它永远不会帮助主人敲一行代码. ...
- flutter initializing gradle终极解决方案
自己开发的公众号,可以领取淘宝内部优惠券 修改flutter.gradle文件 这种做法网上一大堆的教程,如果你还没改过建议先试下,比如这篇 Flutter 运行 一直Initializing gra ...
- Cookie的创建、读取、删除
创建Cookie: HttpCookie cookie = new HttpCookie(COOKIE_NAME_FOR_USER);cookie.Expires = DateTime.Now.Ad ...
- [LeetCode]27. Remove Element移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 2.浅析Hadoop之YARN
YARN也是主从架构,主节点是ResourceManager,从节点是NodeManager,是一种资源分配及任务管理的组件 针对每个任务还有ApplicationMaster应用管理者和Contai ...
- Eclipse SWT
Reference: http://www.eclipse.org/swt/ http://www.functionx.com/win32/Lesson01.htm http://www.win32d ...
- 统计学中的P值与显著性的意义
统计学意义(p值) 结果的统计学意义是结果真实程度(能够代表总体)的一种估计方法.专业上,p值为结果可信程度的一个递减指标,p值越大,我们越不能认为样本中变量的关联是总体中各变量关联的可靠指标.p值是 ...
- CSS3图片边框
CSS3图片边框 顾名思义就是为边框应用背景图片,它和我们常用的background属性比较相似,语法规则:
- Id_Name
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> <be ...
- css:改变滚动条样式
以下亲测谷歌内核的浏览器有用,微软和火狐无效 body::-webkit-scrollbar {/*滚动条整体样式*/ width: 5px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1 ...