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

cell自适应高度的更多相关文章

  1. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  2. TableView cell自适应高度-----xib

    1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExte ...

  3. Cell自适应高度及自定义cell混合使…

    第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...

  4. 自定义 cell 自适应高度

    #import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...

  5. IOS XIB Cell自适应高度实现

    1.代码实现Cell高度自适应的方法 通过代码来实现,需要计算每个控件的高度,之后获取一个cell的 总高度,比较常见的是通过lable的文本计算需要的高度. CGSize labelsize = [ ...

  6. 自定义cell 自适应高度

    #pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...

  7. iOS之UITableView加载网络图片cell自适应高度

    #pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...

  8. iOS UItableview 镶嵌 collectionView ,cell 自适应高度动态布局

    最近在写这个功能,之前看到很多,可是需求一直没有涉及到,大致思路是有的,发现,网上的大部分都有缺陷和bug,我也是好无语啦啦啦,也不晓得是不是升级 了xcode,一样的代码,允许的效果都不一样,,,苦 ...

  9. lable 以及cell的高度自适应

    1.先cell自适应 可以先拿到 这个lable里文字的高度 //lable自适应的高度 -(CGFloat)heightWithString:(NSString *)aString { CGRect ...

随机推荐

  1. Tmux 常用命令与快捷键

    命令与别名 attach-session [-dr] [-t target-session] 别名 attach,连接会话. detach-client [-P] [-a] [-s target-se ...

  2. 队列queue的C实现

    头文件—————————————————————————————— #ifndef _QUEUE_H_ #define _QUEUE_H_ #include <stdlib.h> #def ...

  3. Socket.IO – 基于 WebSocket 构建跨浏览器的实时应用

     Socket.IO 是一个功能非常强大的框架,能够帮助你构建基于 WebSocket 的跨浏览器的实时应用.支持主流浏览器,多种平台,多种传输模式,还可以集合 Exppress 框架构建各种功能复杂 ...

  4. 字符串js编码转换成实体html编码的方法(防范XSS攻击)

    js代码在html页面中转换成实体html编码的方法一: <!DOCTYPE html><html> <head>    <title>js代码转换成实 ...

  5. 开源一个基于nio的java网络程序

    因为最近要从公司离职,害怕用nio写的网络程序没有人能看懂(或许是因为写的不好吧),就调整成了mina(这样大家接触起来非常方便,即使没有socket基础,用起来也不难),所以之前基于nio写的网络程 ...

  6. AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?

    背景 最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/ ...

  7. 【Spark】---- Spark 硬件配置

    存储系统 Spark任务需要从一些外部的存储系统加载数据(如:HDFS 或者 HBase),重要的是存储系统要接近Spark系统,我们有如下推荐:   (1)如果可能,运行Spark在相同的HDFS节 ...

  8. Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)

    我们在项目中可能用到excel表生成,下面的代码就是对excel表的操作: import xlsxwriter import datetime class write_excel(): def __i ...

  9. Qt Style Sheet实践(二):组合框QComboBox的定制

    导读 组合框是一个重要且应用广泛的组件,一般由两个子组件组成:文本下拉单部分和按钮部分.在许多既需要用户选择.又需要用户手动输入的应用场景下,组合框能够很好的满足我们的需求.如我们经常使用的聊天软件Q ...

  10. 移动端前端常见的触摸相关事件touch、tap、swipe等整理

    前端的很多事件在PC端和浏览器端可公用,但有些事件却只在移动端产生,如触摸相关的事件 本文整理了移动端常见的一些事件,包括原生支持的click.touch.tap.swipe事件,也有定义型的gest ...