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. Socket编程指南及示例程序(转)

    1         前言 在一些常用的编程技术中,Socket网络编程可以说是最简单的一种.而且Socket编程需要的基础知识很少,适合初学者学习网络编程.目前支持网络传输的技术.语言和工具繁多,但是 ...

  2. linux shell基础编程2

    while循环 语法1: while [ 条件 ] do 命令序列 done 语法2: while read -r line do 命令序列 done (切记while和左中括号一定要有空格) 例子 ...

  3. SQL:Example Uses of the SUBSTRING String Function

    ---Example Uses of the SUBSTRING String Function --http://www.sql-server-helper.com/tips/tip-of-the- ...

  4. springmvc 登陆拦截器 配合shiro框架使用

    public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{ @Override public boolean pre ...

  5. javascript接入阿里云服务器

    authInfo = r.data.authInfo[0]//后台请求的 var client = new OSS({ region: 'oss-cn-beijing',// 云账号AccessKey ...

  6. list 和 iterate

    list 和 iterate 不同之处 a) list取所有: b) iterate 先取ID,等到用到的时候再根据ID来取对象: c) session 中 list 第二次发出,仍会到数据库查询: ...

  7. xml 转义特殊字符

    写了个request2XML的方法,每当数据中有'<'.'&'符号时,封装的XML就无法解析.发现了XML里的CDATA属性,问题迎刃而解!在XML文档中的所有文本都会被解析器解析. 只 ...

  8. 使用Cloud application Studio在C4C UI里创建下拉列表(dropdown list)

    在Cloud Application Studio里新建一个Code List Data Type: 维护Value和描述信息,以及在ABSL里使用的constant值. 保存之后,上述维护的信息会存 ...

  9. selenium + python(鼠标操作)

    关于最近学习selenium自动化测试鼠标操作的一些总结 常见的鼠标操作

  10. Java里面String的编码问题

    Java里面内置字符串全部是utf-16编码,详细的编码方式看这里 import java.nio.charset.Charset; import java.util.Arrays; import j ...