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 ...
随机推荐
- aliyun ubuntu mysql
http://www.codingyun.com/article/38.html ------------ python install mysql module ubuntu下mysql-pytho ...
- Java知多少(107)几个重要的java数据库访问类和接口
编写访问数据库的Java程序还需要几个重要的类和接口. DriverManager类 DriverManager类处理驱动程序的加载和建立新数据库连接.DriverManager是java.sql包中 ...
- 队列queue的C实现
头文件—————————————————————————————— #ifndef _QUEUE_H_ #define _QUEUE_H_ #include <stdlib.h> #def ...
- VC2013的一个bug
前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其它的非C++元素.这是一个比较新的 ...
- Mergely – 免费的在线文档对比和合并工具
任何类型的文件(无论是否代码),我们可能要比较不同的版本,看发生了什么变化. 有些编辑器都有这个内置功能,其中一些则没有. Mergely 是一个免费使用的 Web 应用程序,帮你你迅速作出文档的差异 ...
- python编码声明的位置很重要
python在3.x版本之前,编码一直是一个很头痛的问题.在代码中如果要使用中文,通常都要在文件的头部注明# -*- coding:utf-8 -*- 这样IDE或者解释器才会智能的转换编码. 这其中 ...
- Winform自定义窗体样式,实现标题栏可灵活自定义
最近在编写C/S结构应用程序时,感觉窗体的标题栏样式太死板了,标题文字不能更改大小.颜色.字体等,按钮不能隐藏等问题,在网上也查找了许多相关的资料,没有找到合适的解决方案,发现许多人也在寻求这个问题, ...
- LVM快照(snapshot)备份
转载自:http://wenku.baidu.com/link?url=cbioiMKsfrxlzrJmoUMaztbrTelkE0FQ8F9qUHX7sa9va-BkkL4amvzCCAKg2hBv ...
- C#写文本日志帮助类(支持多线程)
代码: using System; using System.Configuration; using System.IO; using System.Threading.Tasks; namespa ...
- 在Winform开发框架中,利用DevExpress控件实现数据的快速录入和选择
在实际的项目开发过程中,有好的控件或者功能模块,我都是想办法尽可能集成到我的WInform开发框架中,这样后面开发项目起来,就可以节省很多研究时间,并能重复使用,非常高效方便.在我很早之前的一篇博客& ...