自动计算UITableViewCell高度2(CGRect约束)
2024-08-29 03:20:27
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;
}
最新文章
- srand()以及rand()函数用法
- vs2013卸载后重新安装不能用了,如何解决
- 【POJ 2480】Longge&#39;s problem(欧拉函数)
- page,client,offset区别
- SQL Server优化器特性-隐式谓词
- Python爬虫(图片)编写过程中遇到的问题
- Firefox 对条件判断语句块内的函数声明的处理与其他浏览器有差异
- Java [Leetcode 136]Single Number
- fastica matlab 转载
- 【转】通用分页用户控件(DataGrid,DataList,Repeater都可以用它来分页)
- C#中文本模板(.tt)
- iframe 自适应内容高度
- Go_认识golang
- C++中的纯虚方法
- Sharepoint 2016 - Deploy Office Online Server
- php中判断mysql查询返回结果集是否为空
- Java NIO -2
- 响应式开发(六)-----Bootstrap CSS----------Bootstrap文本排版
- this指向 - Node环境
- #测试框架推荐# test4j,数据库测试