UITableViewCell高度自适应
UITableView是iOS开发过程中常用的列表控件,固定高度的Cell布局再熟悉不过,比较麻烦的是高度不固定的问题,
比如用大量文字需要展示的。
一、手动计算,手动布局
主要的实现思路:计算好需要的高度,然后设置Cell高度和布局;
用的的代理方法的调用顺序
//1、先调用返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//2、再调用返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
关于计算高度的时间:1、在设置高度的时候计算 2、获取到数据初始化Model的时候计算;我选择后者,请求、解析数据、初始化model可以再一次等待过程解决,不需要再获取的时候现计算,速度要快些。创建Model如下:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DataModel : NSObject
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *text;
@property (nonatomic,assign)CGFloat rowHeight;
@end
#import "DataModel.h"
@implementation DataModel
-(void)setText:(NSString *)text
{
_text = text;
//315是文字控件的宽度,这里不涉及适配问题,直说解决思路
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(315, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
self.rowHeight = rect.size.height;
}
@end
这里是在设置文字内容的时候计算高度。
自定义Cell的代码如下
#import <UIKit/UIKit.h>
#import "DataModel.h"
@interface CustomTableCell : UITableViewCell
//文字1
@property (strong, nonatomic) UILabel *name;
//文字2
@property (strong, nonatomic) UILabel *text;
//刷新
-(void)reload:(DataModel *)data;
@end
#import "CustomTableCell.h"
@implementation CustomTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//添加两个UILabel
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
self.name.textAlignment = NSTextAlignmentCenter;
self.name.backgroundColor = [UIColor grayColor];
[self addSubview:self.name];
self.text = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 315, 44)];
self.text.backgroundColor = [UIColor lightGrayColor];
self.text.numberOfLines = 0;
[self addSubview:self.text];
}
return self;
}
-(void)reload:(DataModel *)data
{
self.name.text = data.name;
self.text.text = data.text;
//刷新frame
self.text.frame = CGRectMake(60, 0, 315, data.rowHeight);
}
@end
然后使用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
return data.rowHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identi = @"Cell";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:Identi ];
if (cell == nil) {
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identi];
}
DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
[cell reload:data];
return cell;
}
效果如下图
二、手动计算,自动布局
计算过程同上,但Cell采取自动布局设置如下图
让Name保持距四方都是0,宽度为60;Text保持距四方都是0,numberOfLines为0(自动换行),然后使用
[_tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
cell.name.text = data.name;
cell.text.text = data.text;
cell.text.backgroundColor = [UIColor yellowColor];
return cell;
}
好处是方便做适配。
三、自动计算,自动布局
Cell的自动布局同上(二)中的截图,然后使得自动计算,关键性的API向下支持到iOS7
_tableView.estimatedRowHeight = 44;
_tableView.rowHeight = UITableViewAutomaticDimension;
然后具体实现代码如下
[_tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Cell"];
_tableView.estimatedRowHeight = 44;
_tableView.rowHeight = UITableViewAutomaticDimension;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
DataModel *data = [self.dataSource objectAtIndex:indexPath.row];
cell.name.text = data.name;
cell.text.text = data.text;
return cell;
}
总结:
从手动到自动,必然带来着开发效率的提升,直接对比方法一、三便能感受出来;但是把越多的工作交给系统的API来做,
便失去了响应的可自定义灵活性;比如 “方法一” 中可以获取整个Cell的高度和可以在内容较少时减小Tableview的高度
(如果效果需要),“方法三”便无法知道;其实“方法二”中庸有一的灵活性和三的方便性。文中代码Demo