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
cell自适应高度的更多相关文章
- 自定义cell自适应高度
UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...
- TableView cell自适应高度-----xib
1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExte ...
- Cell自适应高度及自定义cell混合使…
第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...
- 自定义 cell 自适应高度
#import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...
- IOS XIB Cell自适应高度实现
1.代码实现Cell高度自适应的方法 通过代码来实现,需要计算每个控件的高度,之后获取一个cell的 总高度,比较常见的是通过lable的文本计算需要的高度. CGSize labelsize = [ ...
- 自定义cell 自适应高度
#pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...
- iOS之UITableView加载网络图片cell自适应高度
#pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...
- iOS UItableview 镶嵌 collectionView ,cell 自适应高度动态布局
最近在写这个功能,之前看到很多,可是需求一直没有涉及到,大致思路是有的,发现,网上的大部分都有缺陷和bug,我也是好无语啦啦啦,也不晓得是不是升级 了xcode,一样的代码,允许的效果都不一样,,,苦 ...
- lable 以及cell的高度自适应
1.先cell自适应 可以先拿到 这个lable里文字的高度 //lable自适应的高度 -(CGFloat)heightWithString:(NSString *)aString { CGRect ...
随机推荐
- R包介绍
R语言的使用,很大程度上是借助各种各样的R包的辅助,从某种程度上讲,R包就是针对于R的插件,不同的插件满足不同的需求,截至2013年3月6日,CRAN已经收录了各类包4338个. 一. R语言包的安装 ...
- SplayTree伸展树的非递归实现(自底向上)
Splay Tree 是二叉查找树的一种,它与平衡二叉树.红黑树不同的是,Splay Tree从不强制地保持自身的平衡,每当查找到某个节点n的时候,在返回节点n的同时,Splay Tree会将节点n旋 ...
- UML系列03之 UML类图(二)
概要 在"UML系列02之 UML类图(一) "中介绍了类图的基本构成--类的UML表示方法.本文是接着前文,继续对"UML的类图"中几种关系进行介绍.介绍的主 ...
- sql语句查询数据库表结构信息
开发中经常用到查询指定表及其字段的信息,以下是我整理的SQL语句查询方法,供自己平时使用也提供给大家参考! 1.适用MS SQL SERVER: SELECT 表名 = then d.name els ...
- Spring基础——在Spring Config 文件中配置 Bean
一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...
- Sql Server来龙去脉系列之一 目录篇
从工作一直到现在都没怎么花功夫深入学习下Sql Server数据库,在使用Sql Server时90%的时间基本上都是在接触T-SQL,所以数据库这块基本上属于菜鸟级别.至于数据库的底层框架以及运行机 ...
- CSS3的变形transform、过渡transition、动画animation学习
学习CSS3动画animation得先了解一些关于变形transform.过渡transition的知识 这些新属性大多在新版浏览器得到了支持,有些需要添加浏览器前缀(-webkit-.-moz-.- ...
- How to Convert Subversion Repo to Git
用了比较长时间的 SVN,但现在新的项目都采用Git.之前的项目又不得不维护,那么能不能将项目从SVN迁移到Git呢.答案是肯定的,网上的方案是 git-svn,或者更高级的封装 svn2git. 方 ...
- Google的分布式关系型数据库F1和Spanner
F1是Google开发的分布式关系型数据库,主要服务于Google的广告系统.Google的广告系统以前使用MySQL,广告系统的用户经常需要使用复杂的query和join操作,这就需要设计shard ...
- C#中dategridview数据导出为excel文件
先从数据库中获取数据,绑定在datagridview中,再从dategridview中导出为excel文件 1.新建窗体,把控件datagridview和按钮设置好,如图