Masonry和FDTemplateLayoutCell 结合使用示例Demo

我们知道,界面布局可以用Storyboard或Xib结合Autolayout实现,如果用纯代码布局,比较热门的有Masonry、SDAutoLayout,下面的简单demo,采用纯代码布局,实现不定高tableview。

实现方式:Masonry + FDTemplateLayoutCell

先上图:

其中:1和2都是UILabel,内容不固定,所以高度也不固定,3是view,可用作点赞和评论或其他用途。

话不多说,上代码:

1、准备工作

pods集成:

pod ‘UITableView+FDTemplateLayoutCell‘
pod ‘Masonry‘

2、新建cell,由于头像我用的网络图片,要事先集成一下SDWebImage

#import "FDTMasoryCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self initViews];
    }
    return self;
}

-(void)initViews{
    //头像icon
    icon = [[UIImageView alloc] init];
    [self.contentView addSubview:icon];
    //高宽40,顶端和左边距离10px
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.top.equalTo(self.contentView).offset(10);
        make.width.and.height.offset(40);
    }];

    //标题title
    lblTitle = [UILabel new];
    [self.contentView addSubview:lblTitle];
    //高20,左边距离头像10px,顶部距离contentview10px,右边距离15px(为什么是-15,因为ios内原点是左上角,所以右边和底部间距要负数)
    [lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_right).offset(10);
        make.top.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-15);
        make.height.mas_equalTo(20);
    }];

    //描述内容1
    lblDesc = [UILabel new];
    lblDesc.backgroundColor = [UIColor redColor];
    lblDesc.numberOfLines = 0;
    [self.contentView addSubview:lblDesc];
    //不定高label,顶端距离title 10px,左边距离icon 10px, 右边距离 15px
    [lblDesc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblTitle.mas_bottom).offset(10);
        make.left.equalTo(icon.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-15);
    }];

    //描述内容2
    lblDesc2 = [UILabel new];
    lblDesc2.numberOfLines = 0;
    lblDesc2.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:lblDesc2];
    //不定高label,顶端距离描述内容1 10px,左边距离icon 10px, 右边距离 15px
    [lblDesc2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblDesc.mas_bottom).offset(10);
        make.left.equalTo(icon.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-15);
    }];

    //其他
    viewComment = [[UIView alloc] init];
    viewComment.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:viewComment];
    //高25,顶端距离内容2 10px,左边距离和内容2齐平, 右边距离 15px
    [viewComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblDesc2.mas_bottom).offset(10);
        make.left.equalTo(lblDesc2);
        make.height.mas_equalTo(25);
        make.right.bottom.equalTo(self.contentView).offset(-15);
    }];

}

-(void)fill:(FDTModel *)model{
    [icon sd_setImageWithURL:[NSURL URLWithString:model.iconUrl] placeholderImage:[UIImage imageNamed:@"iconDefault"]];
    lblTitle.text = model.title;
    lblDesc.text = model.desc;
    lblDesc2.text = model.desc;
}

  

3、控制器 tableviewview  

#import "FDTMasoryCell.h"
#import "FDTModel.h"
#import "UITableView+FDTemplateLayoutCell.h"
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    //[_tableview registerNib:[UINib nibWithNibName:@"FDTCell" bundle:nil] forCellReuseIdentifier:cellID];
    [_tableview registerClass:[FDTMasoryCell class] forCellReuseIdentifier:cellMasonryID];
    [self.view addSubview:_tableview];

}

#pragma mark - tableview
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArry.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //FDTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    FDTMasoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellMasonryID];
    FDTModel *model = self.dataArry[indexPath.row];
    [cell fill:model];
    return  cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView fd_heightForCellWithIdentifier:cellMasonryID cacheByIndexPath:indexPath configuration:^(id cell) {
        FDTModel *model = self.dataArry[indexPath.row];
        [cell fill:model];
    }];

}

PS:

1、tableview这里用法跟平常一样,唯一区别以及最关键的就是代理内的:heightForRowAtIndexPath方法

这里用到了 UITableView+FDTemplateLayoutCell  处理高度,用法简单明了,再也不用自己根据内容一个个计算了!

2、cell内,我是简单用了三层view,大家也可以按自己需求改动cell即可。

3、这里cell,我是用masonry布局,当然也可以用系统Autolayout添加约束实现布局,有时候cell内如果内容不多,用约束反而更简单。

Last,经过以上三步,一个简单的排列效果就出来了,大家都试试吧。。

有任何疑问,欢迎留言~~~

时间: 2024-08-29 16:47:30

Masonry和FDTemplateLayoutCell 结合使用示例Demo的相关文章

Firefly——dbentrust 示例DEMO (源码+教程)

原地址:http://www.9miao.com/question-15-54002.html Firefly——dbentrust示例说明一.数据库准备本篇示例演示的是firefly与MySQL和memcached之间的数据处理,所以要先准备好数据库.(数据库工具使用的是SQLyogEnt)1.创建数据库 <ignore_js_op> 2.建表下面是一个简单的角色表(player) <ignore_js_op> 二.firefly与MySQL之间的交互(test_dbpool.p

php中钩子(hook)的应用示例demo

我们先来回顾下原本的开发流程:产品汪搞出了一堆需求:当用户注册成功后需要发送短信.发送邮件等等:然后聪明机智勇敢的程序猿们就一扑而上:把这些需求转换成代码扔在 用户注册成功 和 跳转到首页 之间: 没有什么能够阻挡:充满创造力的猿们: <?php class Test{ public function index(){ // 用户注册成功 /* 此处是一堆发送短信的代码 */ /* 此处是一堆发送邮件的代码 */ /* 此处是一堆其他功能的代码 */ // 前往网站首页 } } $test=ne

Android开发之AsyncTask示例Demo

今天做了一个AsyncTask的小Demo,内含注释,通过此Demo,可以对AsyncTask有一个详细的了解 已经将项目上传到了GitHub上(程序有一个小bug,在第一次提交有说明,有解决方法请留言),地址附上: https://github.com/wangpeng0531/AsyncTaskDemo

30 天学 Swift 示例DEMO 集合

      DEMO下载 : http://www.code4app.com/forum.php?mod=viewthread&tid=8817&page=1&extra=#pid114609

微信红包功能(含示例demo)

有空来填坑... 先给示例: https://github.com/zLulus/WeChatRedPacketSample 参考资料:https://www.cnblogs.com/wenhx/p/WeChat-RedPacket-Interface.html 原文地址:https://www.cnblogs.com/Lulus/p/8379739.html

UIPickerView 模块示例demo

UIPickerView 此模块封装了一个可以从屏幕底部向上弹出的 action 选择器(3D滚轮效果).开发者可自定义选择器的样式,包括:导航条颜色.高度,导航条上左右按钮大小.位置.背景色.title,选择器内容区域的背景.高度.字体大小颜色等样式.同时本模块支持自定义是否显示遮罩层及其样式,可监听用户点击遮罩层.导航条左右按钮等事件, 并在监听的事件里自行设置显示/隐藏(show/hide)模块的操作,灵活,多样. 亮点:滚动流畅,自定义数据源,丰富的配置参数. 效果图: open示例代码

Apache Flink 入门示例demo

在本文中,我们将从零开始,教您如何构建第一个Apache Flink (以下简称Flink)应用程序. 开发环境准备 Flink 可以运行在 Linux, Max OS X, 或者是 Windows 上.为了开发 Flink 应用程序,在本地机器上需要有 Java 8.x 和 maven 环境. 如果有 Java 8 环境,运行下面的命令会输出如下版本信息: $ java -versionjava version "1.8.0_65" Java(TM) SE Runtime Envir

selenium - SMTP发送邮件 - 完整示例demo

工程结构如下: test1.py 1 import unittest 2 3 4 class Test(unittest.TestCase): 5 '''我的第一个测试类''' 6 7 @classmethod 8 def setUp(self): 9 pass 10 11 def test_case_1(self): 12 '''测试2是否等于2''' 13 self.assertEqual(2, 2, '不相等2!=2') 14 15 def test_case_2(self): 16 ''

Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api

本文作者:小雷FansUnion-一个有创业和投资经验的资深程序员-全球最大中文IT社区CSDN知名博主-排名第119 实际项目中非常需要写文档,提高Java服务端和Web前端以及移动端的对接效率. 听说Swagger这个工具,还不错,就网上找了些资料,自己实践了下. 一:Swagger介绍 Swagger是当前最好用的Restful API文档生成的开源项目,通过swagger-spring项目 实现了与SpingMVC框架的无缝集成功能,方便生成spring restful风格的接口文档,