cell自适应高度

MyModel.h

 1 #import <Foundation/Foundation.h>
 2 #import <UIKit/UIKit.h>
 3 
 4 @interface MyModel : NSObject
 5 
 6 @property (nonatomic, copy) NSString *textString;
 7 
 8 @property (nonatomic, strong) UIImage *image;
 9 
10 @end

Tool.h

 1 #import <Foundation/Foundation.h>
 2 #import <UIKit/UIKit.h>
 3 
 4 @interface Tool : NSObject
 5 
 6 // 声明类方法来计算文本高度
 7 + (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font;
 8 
 9 
10 // 声明类方法来计算图片高度
11 + (CGFloat)imageHeightWithImage:(UIImage *)image;
12 
13 @end

Tool.m

 1 #import "Tool.h"
 2 
 3 @implementation Tool
 4 
 5 // 计算文本高度
 6 + (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font {
 7     
 8     // ISO7.0中求文本高度的方法,返回一个CGRect的高度
 9     
10     // 第一个参数
11     CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 10000);
12     
13     // 第二个参数:设置以行高为单位
14     
15     
16     CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil];
17     
18     return rect.size.height;
19 }
20 
21 
22 // 计算图片的高度
23 + (CGFloat)imageHeightWithImage:(UIImage *)image {
24     
25     CGFloat width = image.size.width;
26     CGFloat height = image.size.height;
27     
28     // 必须加一个判断,不然有可能会出现问题
29     if (width == 0) {
30         return 0;
31     }
32         
33     return height / width * ([UIScreen mainScreen].bounds.size.width);
34 }
35 
36 @end

MyCell.h

1 #import <UIKit/UIKit.h>
2 
3 @interface MyCell : UITableViewCell
4 
5 @property (nonatomic, strong) UILabel *myLabel;
6 
7 @property (nonatomic, strong) UIImageView *myImageView;
8 
9 @end

MyCell.m

 1 #import "MyCell.h"
 2 #import "Tool.h"
 3 
 4 @implementation MyCell
 5 
 6 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 7     
 8     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 9     if (self) {
10         [self add];
11     }
12     return self;
13 }
14 
15 
16 - (void)add {
17     self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
18     self.myLabel.font = [UIFont systemFontOfSize:20];
19     // 换行显示
20     self.myLabel.numberOfLines = 0;
21     
22     [self.contentView addSubview:self.myLabel];
23     
24     
25     self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.myLabel.frame), self.bounds.size.width, 100)];
26     
27     [self.contentView addSubview:self.myImageView];
28     
29 }
30 
31 /*
32  layoutSubviews在以下情况下回调用
33  1.init初始化方法不会调用,但是addSubViews会调用
34  2.设置view的frame的时候会调用,前提条件是frame前后数据发生变化
35  3.屏幕旋转的时候
36  4.滑动一个scrollView
37  
38  */
39 
40 // 第一步:修改label的高度,根据文本的大小
41 - (void)layoutSubviews {
42     // 1.获取文本高度
43     CGFloat textHeight = [Tool textHeightWithText:self.myLabel.text font:[UIFont systemFontOfSize:20]];
44     
45     // 2.修改label的frame
46     self.myLabel.frame = CGRectMake(0, 0, self.bounds.size.width, textHeight);
47     
48     
49     // 第一步:修改imageView的高度
50     
51     // 1.获取图片的高度
52     CGFloat imageHeight = [Tool imageHeightWithImage:self.myImageView.image];
53     
54     // 2.修改imageView的高度
55     self.myImageView.frame = CGRectMake(0, textHeight, self.bounds.size.width, imageHeight);
56     
57 }
58 
59 @end

RootTableViewController.m

 1 #import "RootTableViewController.h"
 2 #import "MyCell.h"
 3 #import "MyModel.h"
 4 #import "Tool.h"
 5 
 6 @interface RootTableViewController ()
 7 
 8 // 数据model
 9 @property (nonatomic, strong) MyModel *myModel;
10 
11 @end
12 
13 // 定义全局的重用标识符
14 static NSString * const identifier_cell = @"identifier_cell";
15 
16 @implementation RootTableViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     
21     self.myModel = [[MyModel alloc] init];
22     self.myModel.textString = @"刚好和功夫就你家韩国九二五然奇偶为刚好和功夫就你家韩国九二五然奇偶为降温哦人家偶尔我回头让我听后台湾后委托金融我就惹我为奇偶位刚好和功夫就你家韩国九二五然奇偶为降温哦人家偶尔我回头让我听后台湾后委托金融我就惹我为奇偶位降温哦人家偶尔我回头让我听后台湾后委托金融我就惹我为奇偶位";
23     
24     self.myModel.image = [UIImage imageNamed:@"000.jpg"];
25     
26     
27     // 注册cell
28     [self.tableView registerClass:[MyCell class] forCellReuseIdentifier:identifier_cell];
29     
30 }
31 
32 
33 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
34 
35     return 1;
36 }
37 
38 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
39 
40     return 1;
41 }
42 
43 
44 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
45     
46     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier_cell forIndexPath:indexPath];
47     
48     // 设置数据
49     cell.myLabel.text = self.myModel.textString;
50     cell.myImageView.image = self.myModel.image;
51     
52     return cell;
53 }
54 
55 
56 // 第二步:设置cell的高度
57 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
58     
59     CGFloat textHeight = [Tool textHeightWithText:self.myModel.textString font:[UIFont systemFontOfSize:20]];
60     
61     
62     CGFloat imageHeight = [Tool imageHeightWithImage:self.myModel.image];
63     
64     return textHeight + imageHeight;
65 }
66 
67 @end

 

转载于:https://www.cnblogs.com/zhizunbao/p/5422550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值