怎样让自定义Cell的图片和文本自适应高度
Let's do it!
- 首先创建一个Model类
- 包括一个图片名称属性
还有文字内容属性
#import <Foundation/Foundation.h> @interface Model : NSObject
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *info;
@end
创建一个继承于UITableViewCell的MyTableViewCell,把模型作为属性,在MyTableViewCell的延展里写一个UIImageView和UILabel属性
MyTableViewCell.h #import <UIKit/UIKit.h>
@class Model;
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, retain) Model *cellModel;
@end MyTableViewCell.m #import "MyTableViewCell.h"
#import "Model.h"
@interface MyTableViewCell () @property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) UILabel *myLabel;
@end
@implementation MyTableViewCell
- (void)dealloc {
[_cellModel release];
[_myImageView release];
[_myLabel release];
[super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; self.myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_myImageView];
[_myImageView release]; self.myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_myLabel.numberOfLines = ;
[self.contentView addSubview:_myLabel];
[_myLabel release]; return self;
} # pragma mark - override setter
- (void)setCellModel:(Model *)cellModel {
if (_cellModel != cellModel) {
[_cellModel release];
_cellModel = [cellModel retain];
_myImageView.image = [UIImage imageNamed:cellModel.imageName];
_myLabel.text = cellModel.info;
}
} - (void)layoutSubviews {
[super layoutSubviews];
// 首先拿到图片的原尺寸
CGSize size = _myImageView.image.size;
// 用固定的宽度除以图片原宽,得到一个比例
CGFloat scale = self.contentView.frame.size.width / size.width;
// 根据比例求得固定的高度
CGFloat realHeight = size.height * scale;
// 最后设置imageView的frame
_myImageView.frame = CGRectMake(, , self.contentView.frame.size.width, realHeight);
// label的y坐标根据imageView的大小来决定,所以一定写在imageView高度计算之后
_myLabel.frame = CGRectMake(, _myImageView.frame.origin.y + _myImageView.frame.size.height, _myImageView.frame.size.width, );
// sizeToFit根据宽度算高度,所以一定要先有一个宽度(注意label显示行数需要设置为0)
[_myLabel sizeToFit];
}
@end
#import "RootViewController.h"
#import "Model.h"
#import "MyTableViewCell.h" static NSString *const reusableIndentifier = @"cell"; @interface RootViewController ()
<
UITableViewDelegate,
UITableViewDataSource
> @property (nonatomic, retain) NSArray *array; @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad]; Model *model = [[Model alloc] init];
model.imageName = @"Dog4.jpg";
model.info = @"Swift is a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast. Swift 3 is a thorough refinement of the language and the API conventions for the frameworks you use every day. These improvements make the code you write even more natural, while ensuring your code is much more consistent moving forward. For example, select Foundation types such as the new Date type are easier to use and are much faster than previous releases, and the Calendar type uses enums to feel more at home within Swift. SwiftSwiftSwiftSwiftSwiftSwiftSwift"; self.array = @[model]; UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release]; }
# pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableIndentifier];
if (nil == cell) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableIndentifier];
}
cell.cellModel = _array[indexPath.row];
return cell;
} # pragma mark - 设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Model *model = _array[indexPath.row];
UIImage *image = [UIImage imageNamed:model.imageName];
CGSize size = image.size;
CGFloat scale = tableView.bounds.size.width / size.width;
CGFloat realHeight = size.height * scale; // label自适应高度
// 首先定义一个字符变量接收模型里的info属性值
NSString *info = model.info;
// 宽度要和label宽度一样
CGSize infoSize = CGSizeMake(tableView.frame.size.width, );
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:.f]};
// 计算文字高度
// 参数1:自适应尺寸,提供一个宽度,去适应高度
// 参数2:自适应设置(以行为矩形区域自适应,以字体字形自适应)
// 参数3:文字属性,通常这里面需要知道的是字体大小
// 参数4:绘制文本上下文,做底层排版时使用,填nil即可
CGRect infoRect = [info boundingRectWithSize:infoSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
// 图片高度 + 文字高度
// 上面方法在计算文字高度的时候可能得到的是带小数的值,如果用来做视图尺寸的适应的话,需要使用更大一点的整数值
// 取整的方法使用ceil函数
return realHeight + ceil(infoRect.size.height); }
@end
怎样让自定义Cell的图片和文本自适应高度的更多相关文章
- web图片100%宽度自适应,高度不塌陷
一般在web端图片100%自适应,在页面加载的时候存在高度塌陷的问题 解决这个问题其实很简单,用padding-top设置百分比值来实现自适应,公式如下 padding-top = (Image He ...
- iOS学习之UI自定义cell
一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...
- IOS Swift语言开发 tableView的重用以及自cell的自适应高度
http://www.aichengxu.com/iOS/11143168.htm 一.准备数据 (这是一个元组,第一个元素为英雄的名字;第二个元素为英雄头像图片的名字,格式为.PNG,如果为其他的格 ...
- 【原】文本图片自适应高度小bug以及解决办法
自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中
- 文本图片自适应高度小bug以及解决办法
自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中 点击效果: 注:- (void)layoutSubviews 方法不能同时操作,否则会出 ...
- Cell自适应高度及自定义cell混合使…
第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...
- 自定义cell自适应高度
UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...
- iOS开发小技巧--纯代码自定义cell
纯代码自定义cell 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样) 1.新建一个继承自UITableViewCell的子类 2.在initWithStyle:方 ...
- 自定义cell的一些知识
1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...
随机推荐
- mac下执行文件出现Permission Denied的解决
mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...
- JavaScript常用库
提供个学习几种库的url,记录下. 1.JavaScript简介 JavaScript是Netscape公司开发的一种脚本语言(scripting language).JavaScript的出现使得网 ...
- Bullet Physics OpenGL 刚体应用程序模板 Rigid Simulation in Bullet
利用Bullet物理引擎实现刚体的自由落体模拟的模板 Bullet下载地址 Main.cpp #include <GLUT/glut.h> #include <cstdlib> ...
- c++的运算符的重载
1 什么是c++运算符的重载 c++运算符的重载就是说对+.-.>.<等运算符进行重新定义,这样的话,除了基本的类型,所有的类都可以进行基本的运算了,用起来非常方便.特别是用在各种算法中. ...
- Linux就该这么学--命令集合7(管道命令符)
1.管道命令符“|”的作用是将前一个命令的标准输出当作后一个命令的标准输入,格式为:“命令A|命令B”. 找出被限制登录用户的命令是:grep "/sbin/nologin" /e ...
- java内存泄露具体解释
非常多人有疑问,java有非常好的垃圾回收机制,怎么会有内存泄露?事实上是有的,那么何为内存泄露?在Java中所谓内存泄露就是指在程序执行的过程中产生了一些对象,当不须要这些对象时,他们却没有被垃圾回 ...
- ubuntu 12.04 解压安装jdk
ubuntu下解压安装jdk,简单方便.分享一下安装方法: 注:该方法针对新系统,之前没有配置过jdk的情况. 1.下载相应版本号的jdk压缩包.如 jdk-8u5-linux-x64.gz 2.解压 ...
- webform中实现SQL Sever2008数据库数据分页查询
1 分页 1.1 数据库中存储过程 已知 当前页 pageIndex 页容量 pageSize 求 总页数 pageCou ...
- html5--5-5 绘制填充矩形
html5--5-5 绘制填充矩形 学习要点 掌握绘制矩形的方法:strkeRect()/fillRect() 掌握绘制路径的 beginPath()和closePath() 矩形的绘制方法 rect ...
- 使用media来加载css
默认的,css被当做渲染时候必须加载的资源. 设备类型和设备询问允许我们设置一些css资源编程可选的 对于所有的css资源,无论是必须的还是可选的,都会被浏览器加载 The New York Time ...