在iOS 8中沼撕,蘋果引入了UITableView的一項新功能--Self Sizing Cells渔隶,對于不少開發(fā)者來說這是新SDK中一項非常有用的新功能。在iOS 8之前腻格,如果想在表視圖中展示可變高度的動態(tài)內(nèi)容時平委,你需要手動計算行高,而Self Sizing Cells為展示動態(tài)內(nèi)容提供了一個解決方案夺谁。這個方法即對系統(tǒng)cell有效廉赔,也對xib創(chuàng)建的和自定義的cell有效。
1.以前我是這樣計算高度的
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellStr = self.contentAry[indexPath.row];
CGSize cellSize=[cellStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
//cellSize的高度為文字的真實高度匾鸥,需要上下都留出空白蜡塌,這里+30的高度
return cellSize.height+30;
}
如果是簡單的文字,那么很簡單的計算一下就OK了勿负,但是如果附帶圖片馏艾,文字等內(nèi)容呢?
現(xiàn)在研究了一下Self Sizing Cells奴愉,發(fā)現(xiàn)很好用琅摩。
2.系統(tǒng)方法計算高度
在這里我是配合Masonry進(jìn)行坐標(biāo)計算的。
在你創(chuàng)建tableview的地方添加锭硼,使用Self Sizing Cells時需要注意的事項:
1.為原型單元格定義Auto Layout約束
2.指定表視圖的estimatedRowHeight
3.將表視圖的rowHeight屬性設(shè)置為UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100; // 隨便設(shè)個值
self.tableView.rowHeight = UITableViewAutomaticDimension;
下面貼上代碼:
ViewController.h
//
// ViewController.m
// Tableview自適應(yīng)高度
//
// Created by XieHenry on 17/3/8.
// Copyright ? 2017年 XieHenry. All rights reserved.
//
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *contentAry;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"cell自適應(yīng)高度";
self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.estimatedRowHeight = 100;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
self.contentAry = @[@"設(shè)置固定高度100,其余的設(shè)置為自適應(yīng)高度--00",@"測試--01",@"測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試--02",@"測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試--03"];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
/*
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellStr = self.contentAry[indexPath.row];
CGSize cellSize=[cellStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
//cellSize的高度為文字的真實高度房资,需要上下都留出空白,這里+30的高度
return cellSize.height+30;
}
*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//如果有必須的固定高度檀头,可以設(shè)置固定的轰异,沒有固定的,可以設(shè)置自適應(yīng)高度
if (indexPath.row == 0) {
return 100;
} else {
return UITableViewAutomaticDimension;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.contentAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
//利用系統(tǒng)自帶cell類型
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.contentLabel.text = self.contentAry[indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
TableViewCell.h
#import <UIKit/UIKit.h>
@interface TableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *contentLabel;
@end
TableViewCell.m
#import "TableViewCell.h"
#import "Masonry.h"
@implementation TableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self createCell];
}
return self;
}
-(void)createCell{
_contentLabel = [[UILabel alloc]init];
_contentLabel.numberOfLines = 0;
_contentLabel.font = [UIFont systemFontOfSize:13];
[self.contentView addSubview:_contentLabel];
[_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView).offset(10);
make.right.mas_equalTo(self.contentView).offset(-10);
make.top.mas_equalTo(self.contentView).offset(10);
make.bottom.mas_equalTo(self.contentView).offset(-10);
}];
UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:lineView];
[lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.contentView);
make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-0);
make.height.mas_equalTo(0.5);
}];
}
@end
本文Demo暑始。