cell自适应高度

时间:2023-03-08 23:44:03
cell自适应高度

MyModel.h

 #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface MyModel : NSObject @property (nonatomic, copy) NSString *textString; @property (nonatomic, strong) UIImage *image; @end

Tool.h

 #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface Tool : NSObject // 声明类方法来计算文本高度
+ (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font; // 声明类方法来计算图片高度
+ (CGFloat)imageHeightWithImage:(UIImage *)image; @end

Tool.m

 #import "Tool.h"

 @implementation Tool

 // 计算文本高度
+ (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font { // ISO7.0中求文本高度的方法,返回一个CGRect的高度 // 第一个参数
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, ); // 第二个参数:设置以行高为单位 CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil]; return rect.size.height;
} // 计算图片的高度
+ (CGFloat)imageHeightWithImage:(UIImage *)image { CGFloat width = image.size.width;
CGFloat height = image.size.height; // 必须加一个判断,不然有可能会出现问题
if (width == ) {
return ;
} return height / width * ([UIScreen mainScreen].bounds.size.width);
} @end

MyCell.h

 #import <UIKit/UIKit.h>

 @interface MyCell : UITableViewCell

 @property (nonatomic, strong) UILabel *myLabel;

 @property (nonatomic, strong) UIImageView *myImageView;

 @end

MyCell.m

 #import "MyCell.h"
#import "Tool.h" @implementation MyCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self add];
}
return self;
} - (void)add {
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , self.bounds.size.width, )];
self.myLabel.font = [UIFont systemFontOfSize:];
// 换行显示
self.myLabel.numberOfLines = ; [self.contentView addSubview:self.myLabel]; self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.myLabel.frame), self.bounds.size.width, )]; [self.contentView addSubview:self.myImageView]; } /*
layoutSubviews在以下情况下回调用
1.init初始化方法不会调用,但是addSubViews会调用
2.设置view的frame的时候会调用,前提条件是frame前后数据发生变化
3.屏幕旋转的时候
4.滑动一个scrollView */ // 第一步:修改label的高度,根据文本的大小
- (void)layoutSubviews {
// 1.获取文本高度
CGFloat textHeight = [Tool textHeightWithText:self.myLabel.text font:[UIFont systemFontOfSize:]]; // 2.修改label的frame
self.myLabel.frame = CGRectMake(, , self.bounds.size.width, textHeight); // 第一步:修改imageView的高度 // 1.获取图片的高度
CGFloat imageHeight = [Tool imageHeightWithImage:self.myImageView.image]; // 2.修改imageView的高度
self.myImageView.frame = CGRectMake(, textHeight, self.bounds.size.width, imageHeight); } @end

RootTableViewController.m

 #import "RootTableViewController.h"
#import "MyCell.h"
#import "MyModel.h"
#import "Tool.h" @interface RootTableViewController () // 数据model
@property (nonatomic, strong) MyModel *myModel; @end // 定义全局的重用标识符
static NSString * const identifier_cell = @"identifier_cell"; @implementation RootTableViewController - (void)viewDidLoad {
[super viewDidLoad]; self.myModel = [[MyModel alloc] init];
self.myModel.textString = @"刚好和功夫就你家韩国九二五然奇偶为刚好和功夫就你家韩国九二五然奇偶为降温哦人家偶尔我回头让我听后*后委托金融我就惹我为奇偶位刚好和功夫就你家韩国九二五然奇偶为降温哦人家偶尔我回头让我听后*后委托金融我就惹我为奇偶位降温哦人家偶尔我回头让我听后*后委托金融我就惹我为奇偶位"; self.myModel.image = [UIImage imageNamed:@"000.jpg"]; // 注册cell
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:identifier_cell]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier_cell forIndexPath:indexPath]; // 设置数据
cell.myLabel.text = self.myModel.textString;
cell.myImageView.image = self.myModel.image; return cell;
} // 第二步:设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat textHeight = [Tool textHeightWithText:self.myModel.textString font:[UIFont systemFontOfSize:]]; CGFloat imageHeight = [Tool imageHeightWithImage:self.myModel.image]; return textHeight + imageHeight;
} @end