怎样让自定义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 ...
随机推荐
- Redis 3.2.4编译安装
1. 下载安装包 wget url tar zxvf redis-3.2.4.tar.gz 2. 编译安装 cd redis-3.2.4/src/ sudo make && make ...
- jquery live hover
$("table tr").live({ mouseenter: function() { //todo }, mouseleave: function() { //todo } ...
- 【BZOJ2216】[Poi2011]Lightning Conductor 决策单调性
[BZOJ2216][Poi2011]Lightning Conductor Description 已知一个长度为n的序列a1,a2,...,an.对于每个1<=i<=n,找到最小的非负 ...
- EasyRTMP实现Demux解析MP4文件进行rtmp推送实现RTMP直播功能
本文转自EasyDarwin团队Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52965101 前面已经介绍过EasyRTMP,这里不 ...
- ArcGIS10和ArcGIS10.1关于AO Licence初始化的问题
两个版本主要是esriLicenseProductCode.esriLicenseProductCodeArcInfo和esriLicenseProductCode.esriLicenseProduc ...
- Jquery AJAX如何使用Promise/Deferred实现顺序执行?
有的时候有我有N个AJAX请求,第下个请求可能要依赖上个请求的返回值, 可以用 $.ajax("test1.php").then(function(data) { // data ...
- 自定义fragmentlayout
一.抽取视图文件,实例化需要在xml文件中 先上效果图: 1. 编写 xml布局文件 <?xml version="1.0" encoding="utf-8&qu ...
- Python对象拷贝——深拷贝与浅拷贝
对象赋值 浅拷贝 深拷贝 1. 对象赋值 对象的赋值实际上是对对象的引用.也就是说当把一个对象赋值给另一个对象时,只是拷贝了引用.如: >>> t1 = tuple('furzoom ...
- Codeforces Round #398 (Div. 2) C. Garland —— DFS
题目链接:http://codeforces.com/contest/767/problem/C 题解:类似于提着一串葡萄,用剪刀剪两条藤,葡萄分成了三串.问怎样剪才能使三串葡萄的质量相等. 首先要做 ...
- IPFS中文简介
ipfs是什么? 它是一个协议也是一个网络,已经运行了2年半,并非虚无缥缈的空气. 它像比特币网络一样,并没有发明什么新技术,他只是将很多种技术(P2P网络技术,bt传输技术,Git版本控制,自证明文 ...