自动计算UITableViewCell高度2(CGRect约束)
1.先创建model
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface LBDNewMsgListModel : NSObject @property (nonatomic, copy) NSString *activityId;
@property (nonatomic, copy) NSString *activityImg;
@property (nonatomic, copy) NSString *colorType;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *createdAt;
@property (nonatomic, copy) NSString *remark;
@property (nonatomic, copy) NSString *jump;
@property (nonatomic, copy) NSString *title; @property (assign,nonatomic) CGFloat cellHeight; // cell的高度(在本model类中进行计算) @end
.m
#import "LBDNewMsgListModel.h"
#import "LBDMsgCenterCell.h" @interface LBDNewMsgListModel () @property (strong, nonatomic) LBDMsgCenterCell *cell; @end @implementation LBDNewMsgListModel - (LBDMsgCenterCell *)cell
{ if(!_cell){ _cell = [[LBDMsgCenterCell alloc]init];
_cell.model = self;
}
return _cell;
} - (CGFloat)cellHeight
{
if (_cellHeight == ) { _cellHeight = self.cell.cellHeight;
self.cell = nil;
}
return _cellHeight;
}
2.自定义cell
.h
#import <UIKit/UIKit.h>
#import "LBDNewMsgListModel.h" static NSString *cellID = @"LBDMsgCenterCell"; @protocol LBDMessageCenterCellDelagete <NSObject> @optional - (void)returnCellHeight:(CGFloat)height; @end @interface LBDMsgCenterCell : UITableViewCell @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) UILabel *contentLbl;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *lookLbl; @property (nonatomic, strong) LBDNewMsgListModel *model;
+ (instancetype)cellWithTableView:(UITableView *)tableView; @property (nonatomic, weak) id<LBDMessageCenterCellDelagete>delegate; @property (assign,nonatomic) CGFloat cellHeight; // cell的高度 @end
.m
#import "LBDMsgCenterCell.h"
#import "LBDNewMsgListModel.h"
#import "NSString+Extension.h" @implementation LBDMsgCenterCell - (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
} + (instancetype)cellWithTableView:(UITableView *)tableView { LBDMsgCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) { cell = [[LBDMsgCenterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
} cell.selectionStyle = UITableViewCellSelectionStyleNone;
tableView.backgroundColor = [UIColor getColor:@"ececec"];
cell.backgroundColor = [UIColor getColor:@"ececec"]; return cell;
} - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self drawCell];
}
return self;
} - (void)setModel:(LBDNewMsgListModel *)model
{
_model = model; if ([model.colorType isEqualToString:@""]) { _topLineView.hidden = NO;
_msgImgView.hidden = YES;
_topLineView.backgroundColor = [UIColor getColor:@"feb540"];
} else if ([model.colorType isEqualToString:@""]) { _topLineView.hidden = NO;
_msgImgView.hidden = YES;
_topLineView.backgroundColor = [UIColor getColor:@"00ade5"];
} else if ([model.colorType isEqualToString:@""]) { _topLineView.hidden = YES;
_msgImgView.hidden = NO; [_msgImgView sd_setImageWithURL:[NSURL URLWithString:model.activityImg]]; } if ([model.jump isEqualToString:@""]) { _lineView.hidden = YES;
_lookLbl.hidden = YES;
} else if ([model.jump isEqualToString:@""]) { _lineView.hidden = NO;
_lookLbl.hidden = NO;
} _msgDateLbl.text = [Tool dateStrConverWithDateStr:model.createdAt originalType:@"yyyyMMddHHmmss" type:@"yyyy/MM/dd HH:mm"];
_titleLbl.text = model.title;
_contentLbl.text = model.content; [self layoutIfNeeded];
self.cellHeight = CGRectGetMaxY(_bgView.frame); } - (void)drawCell { _msgDateLbl = [[UILabel alloc] init];
_msgDateLbl.text = @"2016/12/25 09:11";
_msgDateLbl.font = FONT();
_msgDateLbl.textColor = [UIColor getColor:@""];
_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 = UIViewContentModeScaleAspectFit;
[_bgView addSubview:_msgImgView]; _topLineView = [[UIView alloc] init];
_topLineView.hidden = YES;
[_bgView addSubview:_topLineView]; _titleLbl = [[UILabel alloc] init];
_titleLbl.font = FONT();
_titleLbl.numberOfLines = ;
_titleLbl.textColor = [UIColor getColor:@""];
[_bgView addSubview:_titleLbl]; _contentLbl = [[UILabel alloc] init];
_contentLbl.font = FONT();
_contentLbl.numberOfLines = ;
_contentLbl.textColor = [UIColor getColor:@""];
[_bgView addSubview:_contentLbl]; _lookLbl = [[UILabel alloc] init];
_lookLbl.textAlignment = NSTextAlignmentRight;;
_lookLbl.text = @"立即查看";
_lookLbl.font = FONT();
[_bgView addSubview:_lookLbl]; _lineView = [[UIView alloc] init];
_lineView.backgroundColor = [UIColor getColor:@"ececec"];
[_bgView addSubview:_lineView]; } - (void)layoutSubviews {
[super layoutSubviews]; _msgDateLbl.frame = CGRectMake(, , SCREEN_WIDTH, ); _bgView.frame = CGRectMake(, CGRectGetMaxY(_msgDateLbl.frame), SCREEN_WIDTH - , ); CGSize titleSize = [_model.title sizeWithFont:FONT() andMaxSize:CGSizeMake(SCREEN_WIDTH - , MAXFLOAT)];
CGSize contentSize = [_model.content sizeWithFont:FONT() andMaxSize:CGSizeMake(SCREEN_WIDTH - , MAXFLOAT)]; if ([_model.colorType isEqualToString:@""] || [_model.colorType isEqualToString:@""]) { _topLineView.frame = CGRectMake(, , SCREEN_WIDTH - , );
_msgImgView.frame = CGRectZero; _titleLbl.frame = CGRectMake(, CGRectGetMaxY(_topLineView.frame) + , SCREEN_WIDTH - , titleSize.height); } else if ([_model.colorType isEqualToString:@""]) { _topLineView.frame = CGRectZero;
_msgImgView.frame = CGRectMake(, , SCREEN_WIDTH - , ); _titleLbl.frame = CGRectMake(, CGRectGetMaxY(_msgImgView.frame) + , SCREEN_WIDTH - , titleSize.height); } _contentLbl.frame = CGRectMake(, CGRectGetMaxY(_titleLbl.frame), SCREEN_WIDTH - , contentSize.height + ); if ([_model.jump isEqualToString:@""]) { _lineView.frame = CGRectZero;
_lookLbl.frame = CGRectZero; _bgView.frame = CGRectMake(, CGRectGetMaxY(_msgDateLbl.frame), SCREEN_WIDTH - , CGRectGetMaxY(_contentLbl.frame)); } else if ([_model.jump isEqualToString:@""]) { _lineView.frame = CGRectMake(, CGRectGetMaxY(_contentLbl.frame), SCREEN_WIDTH - , 0.5);
_lookLbl.frame = CGRectMake(, CGRectGetMaxY(_lineView.frame), SCREEN_WIDTH - , ); _bgView.frame = CGRectMake(, CGRectGetMaxY(_msgDateLbl.frame), SCREEN_WIDTH - , CGRectGetMaxY(_lookLbl.frame)); } }
3.在控制器中使用
.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
LBDNewMsgListModel *model = self.dataMutArr[indexPath.row];
return model.cellHeight;
}
自动计算UITableViewCell高度2(CGRect约束)的更多相关文章
- 使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)
直接上代码: 1:先自定义cell .h文件中 #import <UIKit/UIKit.h> #import "LBDNewMsgListModel.h" #impo ...
- AutoLayout 根据文字、图片自动计算 UITableViewCell 高度
原文网址: http://lvwenhan.com/ios/449.html 此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考 ...
- iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例
一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- UITableViewCell高度自适应探索--AutoLayout结合Frame
UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell地址: http://www.jianshu.com/p/7839e3a273a6UIT ...
- 优化UITableViewCell高度计算的那些事(RunLoop)
这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...
- 《转》优化UITableViewCell高度计算的那些事
我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...
- 优化UITableViewCell高度计算的那些事 by --胡 xu
这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...
- 动态计算UITableViewCell高度
动态计算UITableViewCell高度 UILabel in UITableViewCell Auto Layout - UILabel的属性Lines设为了0表示显示多行.Auto Layout ...
随机推荐
- shell命令修改文件内容
有个 test.txt 文件内容为 hello tom,现在修改成 hello jerry,并保存到test2.txt sed 's/tom/jerry/g' test.txt >test2. ...
- 二、MVC3+EF单表增删改查
document 表为例 写入静态类 NorthwindDataProvider: Controller可直接调用:如 //获取document表全部数据 NorthwindDataProvider. ...
- Android设备之间通过Wifi通信
之前写过PC与Android之间通过WIFI通信(通过Socket,可以在博客里面搜索),PC作为主机,Android作为客户机,现在手头有一台仪器通过wifi传输数据,如果仪器作为主机发射WIFI热 ...
- 正则表达式验证问题(用户名、密码、email、身份证
实现的代码如下: <html> <head> <meta charset="UTF-8"> </head> <body> ...
- HTML <frameset> 标签
<frameset></frameset>:框架标签,可以将页面分割,被frameset标签分割的页面,不允许使用body标签;frameset标签页面内只能出现framese ...
- Android应用开发基础之一:数据存储和界面展现(一)
Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R.java:项目中所有资源文件的资源id Android.jar:Android的jar包,导入此包方可使用 ...
- SOL的补充
之前写过一些关于远程安装系统的文档,但是对于SOL还是糊涂不清. Serial Console 可以将输入输出转发到串行接口(com1, com2), 假如你有串行读取设备,就可以看到显示,控制输入. ...
- 收缩事务日志(sqlserver)
sqlserver数据库的日志文件其实是由很多个逻辑上的日志文件组成,我们可以通过命令看一下数据库日志文件 可以看到的是sqlserver数据库日志文件是由很多文件组成的,当数据库日志文件已满的时候 ...
- C#并行库(TaskParallelLibrary)用法 z
1. Task.Factory.StartNew(() => DoSomeWork());是异步的 下面的代码会先输出ddd,因为Task.Factory.Startnew不阻塞: var ta ...
- UIRecorder安装与使用
继vue单元测试,将进行vue的e2e测试学习. 学习点: 安装uirecorder 用工具(UI Recorder)录制测试脚本 测试脚本的回放 本文意在安装UI Recorder,并且利用该工具进 ...