由sunny大神出品的自動計算UITableviewCell高度
FDTemplateLayoutCell_下載git
目前最流行的AutoLayout框架,比較輕量級
Masonry_下載github
將上述兩個第三方下載後(或者使用Cocoapods)導入工程,而後建立所需文件,此時的工程目錄:框架
MyTableViewCell.h 建立Model屬性,用來傳值佈局
#import <UIKit/UIKit.h> #import "Model.h" @interface MyTableViewCell : UITableViewCell @property (nonatomic, strong)Model *model; @end
MyTableViewCell.m 使用Masonry佈局atom
#import "MyTableViewCell.h" #import "Masonry.h" @implementation MyTableViewCell{ UILabel *_textLB; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self createSubViews]; } return self; } /** * 注意,無論佈局多複雜,必定要有相對於cell.contentView的bottom的約束 */ - (void)createSubViews{ _textLB = [UILabel new]; _textLB.backgroundColor = [UIColor orangeColor]; _textLB.numberOfLines = 0; [self.contentView addSubview:_textLB]; [_textLB mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.equalTo(self.contentView).offset(10); make.bottom.right.equalTo(self.contentView).offset(-10); }]; } /** * 賦值 * * @param model ViewController傳遞過來的Model用來賦值 */ - (void)setModel:(Model *)model{ if (_model != model) { _model = model; _textLB.text = [NSString stringWithFormat:@"%@", model.text]; } }
Model.h 建立數據屬性spa
#import <Foundation/Foundation.h> @interface Model : NSObject @property (nonatomic, copy)NSString *text; @end
Model.m (使用KVC時,若是代碼中的key值不存在,會拋出異常,能夠在類中經過重寫它提供下面的這個方法來解決這個問題)debug
#import "Model.h" @implementation Model - (void)setValue:(id)value forUndefinedKey:(NSString *)key{ } @end
ViewController.hcode
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
ViewController.m 建立列表視圖,並實現自適應高度orm
#import "ViewController.h" #import "MyTableViewCell.h" #import "Model.h" #import "UITableView+FDTemplateLayoutCell.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> @end @implementation ViewController{ NSMutableArray *_allDataArr; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor lightGrayColor]; [self initailData]; [self createMianViews]; } - (void)initailData{ _allDataArr = [NSMutableArray array]; /** * 虛擬數據 */ for (NSInteger i = 0; i < 3; i++) { Model *model = [Model new]; model.text = @"在東方世界裏,挑選小公牛到競技場角鬥有必定的程序。每一頭被帶進場地的公牛都要向手持長矛刺它的鬥牛士發起進攻。其勇敢程度是根據它不顧矛刃的刺痛向鬥牛士進攻的次數來認真評定的"; [_allDataArr addObject:model]; } } - (void)createMianViews{ UITableView *myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; myTableView.backgroundColor = [UIColor whiteColor]; myTableView.delegate = self; myTableView.dataSource = self; myTableView.fd_debugLogEnabled = YES; //打開自適應高度debug模式 [self.view addSubview:myTableView]; [myTableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark -UITableViewDataSource - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; [self setupModelOfCell:cell AtIndexPath:indexPath]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _allDataArr.count; } #pragma mark -UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(id cell) { [self setupModelOfCell:cell AtIndexPath:indexPath]; }]; } #warning 重點(自適應高度必須實現) //預加載Cell內容 - (void)setupModelOfCell:(MyTableViewCell *)cell AtIndexPath:(NSIndexPath *)indexPath{ cell.model = [_allDataArr objectAtIndex:indexPath.row]; } @end