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

最新文章

  1. 线段树 - ZYB&#39;s Premutation
  2. HTML5 使用application cache 接口实现离线数据缓存
  3. bcm cmd
  4. Code First 迁移
  5. 前端js的书写规范和高效维护的方案_自我总结使用的方案
  6. linux bash下 快捷键
  7. NFS挂载故障卡死的问题
  8. linux脚本Shell之awk详解
  9. JavaBean四个作用域范围
  10. hive中 regexp_replace的用法,替换特殊字符问题
  11. maven-shade-plugin插件
  12. Qt5中运行后台网络读取线程与主UI线程互交
  13. 【php增删改查实例】第七节 - 部门管理模块(画一个datagrid表格)
  14. 在Win10下搭建web服务器,使用本机IP不能访问,但是使用localhos或127.0.0.1可以正常访问的解决办法
  15. FileProvider N 7.0 升级 安装APK 选择文件 拍照 临时权限 MD
  16. java命令行执行程序解决依赖外部jar包的问题
  17. Vector/Push_back
  18. [洛谷P1029]最大公约数与最小公倍数问题 题解(辗转相除法求GCD)
  19. MathType中空格个数怎么显示
  20. [SHELL]shell中的数学运算

热门文章

  1. linux下如何安装软件(转载)
  2. HDU 5313 Bipartite Graph(二分图染色+01背包水过)
  3. EasyRTMP内置进入摄像机中实现网络推流直播摄像机的功能
  4. Spring整合Struts2的方法
  5. java集合类学习心得
  6. 点击文本选中checkbox
  7. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(2)
  8. ssh服务器终端乱码
  9. vue指令与$nextTick 操作DOM的不同之处
  10. Java8初体验(1):lambda表达式语法