自动计算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 ...
随机推荐
- BulletedList项目控件基础CSS基础
呈现形态&控件语法: <ul id=“...”> <li></li> . <li></li> . <li></li ...
- js判断下拉框改变状态
<script> $('#questSort').change(function(){ //此处写状态改变要实现的功能 var s=$('#questSort').children('op ...
- PHP常用的一些数组操作总结
1.array_values() :返回包含数组中所有键值的数组,不保留键名. 2.array_diff() 函数返回两个数组的差集数组.该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的 ...
- C#简单实现读取txt文本文件并分页存储到数组
最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...
- scss-变量作用域
SCSS之所以便利,是因为它具有了编程语言的某些特性. 让原本规则刻板的CSS变的灵活起来,下面介绍一下SCSS中的作用域概念. 几乎所有编程语言都有作用域概念的涉及,原理大同小异,SCSS中的也是如 ...
- 什么时候修改class
点击按钮class发生改变 <html> <head> <meta http-equiv="Content-Type" content="t ...
- wxpython CustomTreeCtrl
转自 http://xoomer.virgilio.it/infinity77/Phoenix/lib.agw.customtreectrl.CustomTreeCtrl.html这个网址中有许多控件 ...
- RAC常用日志总结
12C RAC:ocssd启动失败log目录:/u01/app/grid/diag/crs/host02/crs/trace/alert.log
- March 3 2017 Week 9 Friday
Each time you love, love as deeply as if it were forever. 如果爱,请深爱,就像能到地老天荒. If we can only encounter ...
- Excel-怎样实现行列转置
有时候,我们为了某些需要,必须把工作表的行列进行转置的方式显示.重新输入很浪费时间,怎样简单的实现转置呢,强大的excel2007提供了此项功能,具体怎么做,下面看我来演示一下. 工具/原料 装有 ...