Masonry和FDTemplateLayoutCell搭配使用「UITableview自適應內(nèi)容高度」

準備:

1.FDTemplateLayoutCell
FDTemplateLayoutCell
UITableView-FDTemplateLayoutCell
2.Masonry

將上述兩個第三方下載后(或者使用Cocoapods)導入工程,然后創(chuàng)建所需文件,此時的工程目錄:

工程目錄

自定義UITableView

MyTableViewCell.h 創(chuàng)建Model屬性,用來傳值

#import <UIKit/UIKit.h>
#import "Model.h"
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, strong)Model *model;
@end

MyTableViewCell.m 使用Masonry布局

#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數(shù)據(jù)模型

Model.h 創(chuàng)建數(shù)據(jù)屬性

#import <Foundation/Foundation.h>
@interface Model : NSObject
@property (nonatomic, copy)NSString *text;
@end

Model.m (使用KVC時张弛,如果代碼中的key值不存在,會拋出異常酪劫,可以在類中通過重寫它提供下面的這個方法來解決這個問題)

#import "Model.h"
@implementation Model
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}
@end

ViewController視圖控制器

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

@end

ViewController.m 創(chuàng)建列表視圖,并實現(xiàn)自適應高度

#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];
    
    /**
     *  虛擬數(shù)據(jù)
     */
    for (NSInteger i = 0; i < 3; i++) {
        Model *model = [Model new];
        model.text = @"在東方世界里吞鸭,挑選小公牛到競技場角斗有一定的程序。每一頭被帶進場地的公牛都要向手持長矛刺它的斗牛士發(fā)起進攻覆糟。其勇敢程度是根據(jù)它不顧矛刃的刺痛向斗牛士進攻的次數(shù)來認真評定的";
        [_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 重點(自適應高度必須實現(xiàn))
//預加載Cell內(nèi)容
- (void)setupModelOfCell:(MyTableViewCell *)cell AtIndexPath:(NSIndexPath *)indexPath{
    cell.model = [_allDataArr objectAtIndex:indexPath.row];
}
@end

運行結果:

運行結果

重點:

自適應內(nèi)容高度關鍵

復雜視圖:

復雜視圖自適應內(nèi)容高度
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末刻剥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子滩字,更是在濱河造成了極大的恐慌造虏,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件麦箍,死亡現(xiàn)場離奇詭異漓藕,居然都是意外死亡,警方通過查閱死者的電腦和手機挟裂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門享钞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人诀蓉,你說我怎么就攤上這事栗竖∈畲啵” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵狐肢,是天一觀的道長添吗。 經(jīng)常有香客問我,道長处坪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任架专,我火速辦了婚禮同窘,結果婚禮上,老公的妹妹穿的比我還像新娘部脚。我一直安慰自己想邦,他們只是感情好,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布委刘。 她就那樣靜靜地躺著丧没,像睡著了一般。 火紅的嫁衣襯著肌膚如雪锡移。 梳的紋絲不亂的頭發(fā)上呕童,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音淆珊,去河邊找鬼夺饲。 笑死,一個胖子當著我的面吹牛施符,可吹牛的內(nèi)容都是我干的往声。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼戳吝,長吁一口氣:“原來是場噩夢啊……” “哼浩销!你這毒婦竟也來了?” 一聲冷哼從身側響起听哭,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤慢洋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后陆盘,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體且警,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年礁遣,在試婚紗的時候發(fā)現(xiàn)自己被綠了斑芜。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡祟霍,死狀恐怖杏头,靈堂內(nèi)的尸體忽然破棺而出盈包,到底是詐尸還是另有隱情,我是刑警寧澤醇王,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布呢燥,位于F島的核電站,受9級特大地震影響寓娩,放射性物質(zhì)發(fā)生泄漏叛氨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一棘伴、第九天 我趴在偏房一處隱蔽的房頂上張望寞埠。 院中可真熱鬧,春花似錦焊夸、人聲如沸仁连。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽饭冬。三九已至,卻和暖如春揪阶,著一層夾襖步出監(jiān)牢的瞬間昌抠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工鲁僚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留扰魂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓蕴茴,卻偏偏與公主長得像劝评,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子倦淀,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容