【iOS】Masonry和FDTemplateLayoutCell搭配使用「UITableview自適應(yīng)內(nèi)容高度」

本文來自尚妝iOS團隊嘉文
發(fā)表于尚妝github博客,歡迎訂閱!

準備:

1.FDTemplateLayoutCell
FDTemplateLayoutCell
UITableView-FDTemplateLayoutCell
2.Masonry

將上述兩個第三方下載后(或者使用Cocoapods)導(dǎo)入工程,然后創(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;
}
/**
 *  注意,不管布局多復(fù)雜,一定要有相對于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)自適應(yīng)高度

#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;       //打開自適應(yīng)高度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 重點(自適應(yīng)高度必須實現(xiàn))
//預(yù)加載Cell內(nèi)容
- (void)setupModelOfCell:(MyTableViewCell *)cell AtIndexPath:(NSIndexPath *)indexPath{
    cell.model = [_allDataArr objectAtIndex:indexPath.row];
}
@end

運行結(jié)果:

運行結(jié)果

重點:

自適應(yīng)內(nèi)容高度關(guān)鍵

復(fù)雜視圖:

復(fù)雜視圖自適應(yīng)內(nèi)容高度
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 準備: 1.FDTemplateLayoutCell 由sunny大神出品的自動計算UITableviewCell...
    LuisX閱讀 5,154評論 15 49
  • 親愛的咪貝, 端午小長假的這兩天,我們都宅在了家。第一天你說過得很愉快,媽媽也這么覺得。而第二天,能量明顯下滑,這...
    葛瑛閱讀 184評論 1 1
  • “我喜歡你?!倍嗝疵篮玫乃膫€字,可被有些人說出來就顯得那么廉價。 剛認識不到48小時,他就能跟你說出各種花樣的情話...
    尋覓一樣的你閱讀 456評論 0 0
  • 第一次在外地,明天是父親的生日,今天母親卻偷偷的打來電話,告訴我不要忘了父親的生日……,我的心突然一緊,不禁自問,...
    南枳垚垚閱讀 107評論 0 1
  • 伴著雨聲睡著了 醒來還在聽著滴答滴答的聲音 雨是心里某個角落里的牽念 喜歡雨也許是與生俱來 喜歡雨的綿綿不休 輕輕...
    田萍閱讀 352評論 3 3

友情鏈接更多精彩內(nèi)容