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的图片和文本自适应高度的更多相关文章

  1. web图片100%宽度自适应,高度不塌陷

    一般在web端图片100%自适应,在页面加载的时候存在高度塌陷的问题 解决这个问题其实很简单,用padding-top设置百分比值来实现自适应,公式如下 padding-top = (Image He ...

  2. iOS学习之UI自定义cell

    一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...

  3. IOS Swift语言开发 tableView的重用以及自cell的自适应高度

    http://www.aichengxu.com/iOS/11143168.htm 一.准备数据 (这是一个元组,第一个元素为英雄的名字;第二个元素为英雄头像图片的名字,格式为.PNG,如果为其他的格 ...

  4. 【原】文本图片自适应高度小bug以及解决办法

    自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中

  5. 文本图片自适应高度小bug以及解决办法

    自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中 点击效果: 注:- (void)layoutSubviews 方法不能同时操作,否则会出 ...

  6. Cell自适应高度及自定义cell混合使…

    第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...

  7. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  8. iOS开发小技巧--纯代码自定义cell

    纯代码自定义cell 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样) 1.新建一个继承自UITableViewCell的子类 2.在initWithStyle:方 ...

  9. 自定义cell的一些知识

    1.要往cell里面添加一个自定义的子控件,都是添加到cell的contentView,不是添加到cell里面. 2.通过xib自定义cell * 添加tableView * 加载团购数据 * 新建x ...

随机推荐

  1. leetcode题目解答报告(1)

    Remove Element 题目: Given an array and a value, remove all instances of that value in place and retur ...

  2. Extjs-树 Ext.tree.TreePanel 动态加载数据

    先上效果图 1.说明Ext.tree.Panel 控件是树形控件,大家知道树形结构在软件开发过程中的应用是很广泛的,树形控件的数据有本地数据.服务器端返回的数据两种.对于本地数据的加载,在extjs的 ...

  3. file descriptor 0 1 2 一切皆文件 stdout stderr stdin /dev/null 沉默是金 pipes

    $>emtry_or_create_a_file.f $ll>>append_a_file.f standard output input error $ls -l /usr/bin ...

  4. python网络爬虫之使用scrapy下载文件

    前面介绍了ImagesPipeline用于下载图片,Scrapy还提供了FilesPipeline用与文件下载.和之前的ImagesPipeline一样,FilesPipeline使用时只需要通过it ...

  5. Appium——详解Appium server capabilities

      appium server capabilities来告诉appium,如何运行自动化测试,因此需要详细了解. 官方文档:http://appium.io/slate/en/master/?rub ...

  6. 记录下 hubot相关

    适配器工厂https://hubot.github.com/docs/adapters/ 自己写适配器https://hubot.github.com/docs/adapters/developmen ...

  7. linux下把命令执行的结果输出

    我们知道在linux下当我们想把文字用命令输入到一个文本下时可以用echo命令 例:echo "nihao" > /z.txt   同样当我们想把命令执行的结果也输入到一个文 ...

  8. iOS 键盘遮挡输入框万能解决方案(多个输入框)

    效果图如下: 思路分析: 代码: 知识点: 问题: 效果图如下: 思路分析: 当我们有很多输入框时,有时候键盘弹出来会遮挡着输入框.我们需要获取输入框和键盘相对于最外层视图的位置来判断是否遮挡,如果遮 ...

  9. hdu 1361.Parencodings 解题报告

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence.   P-se ...

  10. plsql导入cvs 时提示missing right parenthesis

    删除自动生成的时间格式值,如:SQL function框里自动生成的值