python tableview 自适应_【iOS】Masonry和FDTemplateLayoutCell搭配使用「UITableview自适应内容高度」...

本文来自尚妆iOS团队嘉文

发表于尚妆github博客,欢迎订阅!

准备:

1.FDTemplateLayoutCell

由sunny大神出品的自动计算UITableviewCell高度

FDTemplateLayoutCell_下载

1c2c46d3eb367678ec72f711b9d9e275.png

f0712c9111f96dfe6cbe0782ca697c55.gif

2.Masonry

目前最流行的AutoLayout框架,比较轻量级

Masonry_下载

将上述两个第三方下载后(或者使用Cocoapods)导入工程,然后创建所需文件,此时的工程目录:

cf0f032a4b9fa0fe9a07ee42c8807e40.png

自定义UITableView

MyTableViewCell.h 创建Model属性,用来传值

#import

#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数据模型

Model.h 创建数据属性

#import

@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

@interface ViewController : UIViewController

@end

ViewController.m 创建列表视图,并实现自适应高度

#import "ViewController.h"

#import "MyTableViewCell.h"

#import "Model.h"

#import "UITableView+FDTemplateLayoutCell.h"

@interface ViewController ()

@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

运行结果:

faf88fced8b6d3aa471b9161f8bb52a1.png

重点:

eb6eab63a3c18d5b6ac88f41aae82230.png

复杂视图:

81cf8457955e4c89abb9d1e563f57cb1.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值