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. Git 文件比较

    Git 的三个作业场: 工作区(Work Tree) 项目根目录下 .git 目录以外所有区域,是编辑项目文件的地方. 缓存区(Index) 工作区文件必须先保存在缓存区,之后从缓存区保存到仓库. 仓 ...

  2. JS基础回顾,小练习(判断数组,以及函数)

    追梦子博客版权所有. // 判断arr是否为一个数组,返回一个bool值 方法1: function isArray(arr) { var str = arr.__proto__.constructo ...

  3. Exchange Server简介与搭建

    一.Exchange Server简介Exchange Server 是微软公司的一套电子邮件服务组件,是个消息与协作系统. 简单而言,Exchange server可以被用来构架应用于企业.学校的邮 ...

  4. 在SQL Server里如何进行页级别的恢复

    在今天的文章里我想谈下每个DBA应该知道的一个重要话题:在SQL Server里如何进行页级别还原操作.假设在SQL Server里你有一个损坏的页,你要从最近的数据库备份只还原有问题的页,而不是还原 ...

  5. springMVC源码分析之拦截器

    一个东西用久了,自然就会从仅使用的层面上升到探究其原理的层面,在javaweb中springmvc更是如此,越是优秀的框架,其底层实现代码更是复杂,而在我看来,一个优秀程序猿就相当于一名武林高手,不断 ...

  6. 高级四则运算器—结对项目总结(193 &105)

    高级四则运算器—结对项目总结 为了将感想与项目经验体会分割一下,特在此新开一篇博文. 界面设计 啥都不说,先上图震慑一下... 上面的三个界面是我们本次结对项目的主界面,恩,我也觉得挺漂亮的!你问我界 ...

  7. POI中getLastRowNum() 和getLastCellNum()的区别 hssfSheet.getLastRowNum();//最后一行行标,比行数小1 hssfSheet.getRow(k).getLastCellNum();//获取列数,比最后一列列标大1

    hssfSheet.getLastRowNum();//最后一行行标,比行数小1 hssfSheet.getRow(k).getLastCellNum();//获取列数,比最后一列列标大1

  8. easyui-treegrid节点选择

    easyui-treegrid本身不能实现选中父节点子节点全选,必须通过另外的方法来实现,这里说下如何通过修改节点样式添加checkbox来实现级联选择效果 首先需要格式化节点的样式 formatte ...

  9. 使用innerHTML获取HTML代码时,HTML标记属性的双引号好多都消失不见了,原来是属性值中包含空格才会保留双引号

    最近搞的一个项目中所使用的方式比较奇怪,用Label显示HTML内容,然后不断地使用JS把Label的innerHTML复制到TextBox中. 但是,昨天发现了一个问题,获取元素值的时候,有时候正常 ...

  10. Unity3D-terrain brush地形画刷无法出现在Scene中,无法刷地图2

    原因大概是 画刷brush 太小了,地图也太小了,没出出现. 如图,非正常状态: 解决方法: tag: terrain brush not working unity