很多情況下TableView 的高度要隨著cell 的內(nèi)容的變化而改變,今天試了下Autolayout 讓cell自適應(yīng)高度。
首先我們實(shí)現(xiàn)一個(gè)控制器,創(chuàng)建一個(gè)UITableView
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "YMTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation ViewController
{
UITableView * mytabView;
NSArray * dataSource;
}
- (void)viewDidLoad {
[super viewDidLoad];
//數(shù)據(jù)源
dataSource = [self getData];
mytabView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
mytabView.delegate = self;
mytabView.rowHeight = UITableViewAutomaticDimension;
mytabView.estimatedRowHeight = 44;//期望高度
mytabView.dataSource = self;
[self.view addSubview:mytabView];
}
#pragma mark - TableView DataSource delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * reuseID = @"YMTableViewCell";
YMTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"YMTableViewCell" owner:nil options:nil] firstObject];
}
//獲取數(shù)據(jù)源
NSDictionary * model = dataSource[indexPath.row];
cell.title = model[@"title"];
cell.imgUrl = model[@"imgUrl"];
return cell;
}
-(NSArray *)getData
{
return @[
@{@"title":@"鍵路徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相對(duì)于其前面的性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
@{@"title":@"鍵路徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相對(duì)于其前面的性質(zhì)。鍵路徑使您可以以獨(dú)立于模型實(shí)現(xiàn)的方式指定相關(guān)對(duì)象的性質(zhì)。通過鍵路徑,您可以指定對(duì)象圖中的一個(gè)任意深度的路徑,使其指向相關(guān)對(duì)象的特定屬性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
@{@"title":@"鍵路徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相對(duì)于其前面的性質(zhì)。鍵路徑使您可以以獨(dú)立于模型實(shí)現(xiàn)的方式指定相關(guān)對(duì)象的性質(zhì)。通過鍵路徑,您可以指定對(duì)象圖中的一個(gè)任意深度的路徑,使其指向相關(guān)對(duì)象的特定屬性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
@{@"title":@"鍵路徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相關(guān)對(duì)象的性質(zhì)。通過鍵路徑,您可以指定對(duì)象圖中的一個(gè)任意深度的路徑,使其指向相關(guān)對(duì)象的特定屬性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
@{@"title":@"鍵路徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
@{@"title":@"鍵路徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相對(duì)于其前面的性質(zhì)。鍵路徑使您可以以獨(dú)立于模型實(shí)現(xiàn)的方式指定相關(guān)對(duì)象的性質(zhì)。通過鍵路徑,您可以指定對(duì)象圖中的一個(gè)任意深度的路徑,使其指向相關(guān)對(duì)象的特定屬性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
@{@"title":@"鍵路徑徑是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相對(duì)于其前面的性質(zhì)是一個(gè)由用點(diǎn)作分隔符的鍵組成的字符串,用于指定一個(gè)連接在一起的對(duì)象性質(zhì)序列。第一個(gè)鍵的 性質(zhì)是由先前的性質(zhì)決定的,接下來每個(gè)鍵的值也是相對(duì)于其前面的性質(zhì)。鍵路徑使您可以以獨(dú)立于模型實(shí)現(xiàn)的方式指定相關(guān)對(duì)象的性質(zhì)。通過鍵路徑,您可以指定對(duì)象圖中的一個(gè)任意深度的路徑,使其指向相關(guān)對(duì)象的特定屬性",@"imgUrl":@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg"},
];
}
@end
接下來我問用xib 定制一個(gè)TableViewCell ,如下圖,其中中間的lable設(shè)置約束上下左右距離固定,Lines為0這樣lable才能根據(jù)內(nèi)容來自適應(yīng)自己的高度,最下面對(duì)View目的是用來裝圖片的,它的約束是上下左右間距固定,高度固定

屏幕快照 2016-12-15 下午4.20.53.png

屏幕快照 2016-12-15 下午4.24.12.png
下面來看下YMTableViewCell是怎么寫的
.h文件
#import <UIKit/UIKit.h>
@interface YMTableViewCell : UITableViewCell
@property (nonatomic,copy) NSString * title;//lable 的內(nèi)容
@property (nonatomic,copy) NSString * imgUrl;//圖片的鏈接
@end
.m文件
#import "YMTableViewCell.h"
@implementation YMTableViewCell
{
__weak IBOutlet NSLayoutConstraint *bgViewHeigth;//下方圖片BGView的高度約束
__weak IBOutlet UIView *bgView;
__weak IBOutlet UILabel *titleLb;
__weak IBOutlet UIImageView *imgView;
}
-(void)setTitle:(NSString *)title
{
if (title) {
_title = title;
titleLb.text = _title;
[titleLb sizeToFit];
}
}
-(void)setImgUrl:(NSString *)imgUrl
{
if (imgUrl) {
_imgUrl = imgUrl;
//子線程網(wǎng)絡(luò)請(qǐng)求圖片,主線程刷新UI ,避免造成TableView 卡頓
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imgUrl]]];
dispatch_async(dispatch_get_main_queue(), ^{
imgView.image = img;
});
});
}
}
- (void)awakeFromNib {
[super awakeFromNib];
NSArray * arr = @[
@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3637379975,3338374522&fm=21&gp=0.jpg",
@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg",
@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3365690640,4217511699&fm=11&gp=0.jpg",
@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2116183623,2547584189&fm=11&gp=0.jpg",
@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3637379975,3338374522&fm=21&gp=0.jpg",
@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2068189867,652372891&fm=21&gp=0.jpg",
@"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3365690640,4217511699&fm=11&gp=0.jpg",
@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2116183623,2547584189&fm=11&gp=0.jpg",
@"http://img2.imgtn.bdimg.com/it/u=984366901,3085090843&fm=11&gp=0.jpg",
@"http://img4.imgtn.bdimg.com/it/u=3137969434,2784896854&fm=11&gp=0.jpg",
@"http://img4.imgtn.bdimg.com/it/u=2297567659,3444652177&fm=23&gp=0.jpg",
@"http://img3.imgtn.bdimg.com/it/u=686859721,4258685574&fm=23&gp=0.jpg",
@"http://img2.imgtn.bdimg.com/it/u=3749387269,1232254169&fm=23&gp=0.jpg",
@"http://img3.imgtn.bdimg.com/it/u=2577062333,855172360&fm=11&gp=0.jpg"];
//隨機(jī)創(chuàng)建0-9個(gè)UIImageView
NSInteger count = arc4random()%9+1;
NSLog(@"count == %ld",count);
for (NSInteger i = 0; i < count; ++i) {
UIImageView * picView = [[UIImageView alloc] initWithFrame:CGRectMake(0+ 60 *(i%3), 60*(i/3), 50, 50)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[i]]]];
//主線程刷新
dispatch_async(dispatch_get_main_queue(), ^{
picView.image = img;
});
});
[bgView addSubview:picView];
}
//給 bgViewHeigth.constant 賦值
if (1 <= count && count <=3) {
bgViewHeigth.constant = 60 * 1;
}else if (4 <= count && count <= 6){
bgViewHeigth.constant = 60 * 2;
}else if (7 <= count && count <= 9){
bgViewHeigth.constant = 60 * 3;
}else{
bgViewHeigth.constant = 0;
}
}
@end
附上下過圖

效果圖.gif