美文网首页iOS开发技术JC专题mas
Masonry和FDTemplateLayoutCell搭配使用

Masonry和FDTemplateLayoutCell搭配使用

作者: LuisX | 来源:发表于2016-04-25 11:25 被阅读2932次

准备:

1.FDTemplateLayoutCell
FDTemplateLayoutCell UITableView-FDTemplateLayoutCell
2.Masonry

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

工程目录

自定义UITableView

MyTableViewCell.h 创建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数据模型

Model.h 创建数据属性

#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 创建列表视图,并实现自适应高度

#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

运行结果:

运行结果

重点:

自适应内容高度关键

复杂视图:

复杂视图自适应内容高度

相关文章

网友评论

  • 杰克道长:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(id cell) {
    [self setupModelOfCell:cell AtIndexPath:indexPath];
    }];
    }

    在计算高度中调用方法:

    - (void)setupModelOfCell:(MyTableViewCell *)cell AtIndexPath:(NSIndexPath *)indexPath{
    cell.model = [_allDataArr objectAtIndex:indexPath.row];
    }

    这样出错。 不能用点语法
  • 请叫我小白同学:大哥,复杂视图如何自动算出来?就是让能内容撑起contentView,contentview上最后一个控件是一些图片,但是图片的高度需要我自己给出来!大哥请指点一下,cell控件上的图片(九宫格图片)如何计算出高
  • blazer_iOS:为什么创建UITableView的时候,一定要使用写死Frame值,而不能把TableView也用Masonry来进行约束,
    blazer_iOS:@LuisX 好的,谢谢
    LuisX:@blazer_iOS 也可以不写死frame的,frame你设置为CGRectZero,然后下面用masonry来约束UITableView
  • Rickie_Lambert:github 地址, 求楼主 给一份, 谢谢, 楼主 好人 一生平安
  • 西门欥雪:gitHub地址是多少,求demo fwm0602@qq.com.
  • feng_dev:图片如果想 宽度 就是 屏幕 宽度 ,content Model 设置 成 aspect fill , 怎么设置约束能让 图片显示 他的高度啊,图片 如果没有 设置 clip 就 被切了很多, 设置了,会遮挡 文字,求助
  • feng_dev:好像直接在 height for row 里面写 cell.model = self.dataArray[indexPath.row]; 就不行啊
  • feng_dev:大神,写的真好,有个问题,那个库里面 不是有三个方法,一般用哪个最好啊?看的不是太懂,能解释下三个方法吗
  • 马爷:来1块钱的代码
  • 这小歌不错:服务员来一份源码。
  • 大华日记:首页推荐这些有人看吗?还是小编是技术喜欢推荐这些?简书到现在自己都没定位清楚吧。看看首页都是推荐什么东西?官方的广告几篇,几篇矫情的文章,剩下的都是技术的。不是一直在招产品经理,还没招到吗?
  • 听小马儿说:大神,你的复杂视图: 的代码没有放出来吗

本文标题:Masonry和FDTemplateLayoutCell搭配使用

本文链接:https://www.haomeiwen.com/subject/pyqfrttx.html