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约束)的更多相关文章

  1. 使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)

    直接上代码: 1:先自定义cell .h文件中 #import <UIKit/UIKit.h> #import "LBDNewMsgListModel.h" #impo ...

  2. AutoLayout 根据文字、图片自动计算 UITableViewCell 高度

    原文网址: http://lvwenhan.com/ios/449.html 此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考 ...

  3. iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例

    一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...

  4. 优化UITableViewCell高度计算的那些事

    优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...

  5. UITableViewCell高度自适应探索--AutoLayout结合Frame

    UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell地址: http://www.jianshu.com/p/7839e3a273a6UIT ...

  6. 优化UITableViewCell高度计算的那些事(RunLoop)

    这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...

  7. 《转》优化UITableViewCell高度计算的那些事

    我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...

  8. 优化UITableViewCell高度计算的那些事 by --胡 xu

    这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...

  9. 动态计算UITableViewCell高度

    动态计算UITableViewCell高度 UILabel in UITableViewCell Auto Layout - UILabel的属性Lines设为了0表示显示多行.Auto Layout ...

随机推荐

  1. 使用Git(msysgit)和TortoiseGit上传代码到GitHub

    1.准备 下载Git for Windows (msysgit) 下载TortoiseGit 安装过程很简单,一直点击下一步到完成即可. 2.配置TortoiseGit 1.双击TortoiseGit ...

  2. React.js学习小结

    最近一段时间都在学习React.js,感觉还不错,现在把自己的一些学习笔记记录一下,留着以后学习查看. 0.React全家桶(技术栈) 1.React主体 2.WebPack:grunt.gulp自动 ...

  3. 2017年11月4日 vs类和结构的区别&哈希表&队列集合&栈集合&函数

    类和结构的区别 类: 类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存 类有构造和析构函数 类可以继承和被继承 结构: 结构是值类型在栈上分配(虽然栈的访问速度比 ...

  4. HDU 1003 最大连续和

    http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html 这里难点只有求起始位置,把握状态变化就行.一般这种子序列问题,都可以用dp简化 #include ...

  5. TypeScript 入门笔记

    1.原始数据类型 JavaScript 的类型分为两种:原始数据类型和对象数据类型.原始数据类型包括布尔值.数值.字符串.null.undefined 以及 ES6 中的 Symbol. 前五种数据类 ...

  6. cf567E. President and Roads(最短路计数)

    题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...

  7. C语言字符数组与字符串

    研究几个案例: 输出图案: #include <stdio.h> void main() { ][] = { {', ' ', ' '}, {', ' '}, {'}, {', ' '}, ...

  8. python 多线程效果演示

    多线程演示 不使用多线程的情况 import threading import time def run(n): print("task ",n) time.sleep(2) ru ...

  9. tensorflow读取jpg格式图片报错 ValueError: Only know how to handle extensions: ['png']; with Pillow installed matplotlib can handle more images

    当运行mpimg.imread("img.jpg")时,spyder 出现如下错误: ValueError: Only know how to handle extensions: ...

  10. Python类三种方法,函数传参,类与实例变量(一)

    1 Python的函数传递: 首先所有的变量都可以理解为内存中一个对象的'引用' a = 1 def func(a): a = 2 func(a) print(a) # 1 a = 1 def fun ...