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 = 0; [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(0, 0, self.contentView.frame.size.width, realHeight); // label的y坐标根据imageView的大小来决定,所以一定写在imageView高度计算之后 _myLabel.frame = CGRectMake(0, _myImageView.frame.origin.y + _myImageView.frame.size.height, _myImageView.frame.size.width, 40); // 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, 1000); NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:17.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