使用FDTemplateLayout框架打造个性App

效果展示

project下载地址

·

进入构建结构

首先我们新建一个project

接下来我们拖进来一个Table View Controller,将Storyboard Entry Point指向我们的Table View Controller。

原来的ViewController就能够删除了。

效果如图所看到的

选中Table View Controller,点击上面菜单条中Editor->Embed in->Navigation Controller

主要的工作我们都做完了。能够讲project中无用的东西都能够删除了,方便我们进行编写东西

主题(代码编写)

在这里我进行代码的编写,讲述主要部分,后面会把完整的项目给出

构建LDEntity用来初始化字典,进行Json数据的解析

LDEntity.h

#import <Foundation/Foundation.h>

@interface FDFeedEntity : NSObject

- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *username;
@property (nonatomic, copy) NSString *time;
@property (nonatomic, copy) NSString *imageName;

@end

-(instancetype)initWithDictionary:(NSDictionary *)dictionary方法的详细实现,返回本身类型

LDEntity.m

#import "FDFeedEntity.h"

@implementation FDFeedEntity

- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = super.init;
    if (self) {
        self.title = dictionary[@"title"];
        self.content = dictionary[@"content"];
        self.username = dictionary[@"username"];
        self.time = dictionary[@"time"];
        self.imageName = dictionary[@"imageName"];
    }
    return self;
}

@end

以上代码的功能主要是为了解析JSON数据

构建我们的Main.storyboard

选中Table View更改Style为Grouped

设计出以下样式,而且使用 auto layout进行约束

接下来编写cell

新建LDCell 继承 UITableViewCell

将Main.storyboard中TableView中的Cell的Identifier设置为LDCell,而且关联LDCell

LDCell.h

构建一个LDEntity对象解析数据

#import <UIKit/UIKit.h>
#import "LDEntity.h"

@interface LDCell : UITableViewCell

@property(nonatomic,strong)LDEntity *entity;

@end

将刚才的全部控件与LDCell.m关联

在LDCell.m中构建entity的set方法,还有须要注意的是假设你没有使用auto layout,你须要重写约束方法

LDCell.m

#import "LDCell.h"

@interface LDCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIImageView *contentImageView;
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation LDCell

- (void)awakeFromNib {
    // 修复IOS7中的BUG- 初始化 constraints warning

    self.contentView.bounds = [UIScreen mainScreen].bounds;
}

//关联控件进行数据显示
-(void)setEntity:(LDEntity *)entity{
    _entity = entity;

    self.titleLabel.text = entity.title;
    self.contentLabel.text = entity.content;
    self.contentImageView.image = entity.imageName.length > 0 ?

[UIImage imageNamed:entity.imageName] : nil;
    self.usernameLabel.text = entity.username;
    self.timeLabel.text = entity.time;
}

#if 0

// 假设没有使用 auto layout, 请重写这种方法
- (CGSize)sizeThatFits:(CGSize)size
{
    CGFloat totalHeight = 0;
    totalHeight += [self.titleLabel sizeThatFits:size].height;
    totalHeight += [self.contentLabel sizeThatFits:size].height;
    totalHeight += [self.contentImageView sizeThatFits:size].height;
    totalHeight += [self.usernameLabel sizeThatFits:size].height;
    totalHeight += 40; // margins
    return CGSizeMake(size.width, totalHeight);
}

#endif

@end

開始编写我们最重要的类LDViewController.在编写这个类之前。我们须要使用FDTemplateLayout

,而且将TableViewController与LDViewController进行关联

LDViewController.h

#import <UIKit/UIKit.h>

@interface FDFeedViewController : UITableViewController

@end

TableView的使用有不明确的能够去学习下,这里不做解释了。凝视中给出了重点的地方解说,设置data source以及设置Delegate在程序中做出区分

LDViewController.m


#import "LDViewController.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "LDEntity.h"
#import "LDCell.h"

@interface LDViewController ()<UIActionSheetDelegate>

@property (nonatomic, copy) NSArray *prototypeEntitiesFromJSON;
@property (nonatomic, strong) NSMutableArray *feedEntitySections; // 2d array
@property (nonatomic, assign) BOOL cellHeightCacheEnabled;

@end

@implementation LDViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 200;
    self.tableView.fd_debugLogEnabled = YES;

    self.cellHeightCacheEnabled = YES;

    [self buildTestDataThen:^{
        self.feedEntitySections = @[].mutableCopy;
        [self.feedEntitySections addObject:self.prototypeEntitiesFromJSON.mutableCopy];
        [self.tableView reloadData];
    }];
}

- (void)buildTestDataThen:(void (^)(void))then
{
    // 模拟一个异步请求
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // 读取数据从 `data.json`
        NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
        NSData *data = [NSData dataWithContentsOfFile:dataFilePath];
        NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *feedDicts = rootDict[@"feed"];

        // 讲数据转化为 `LDEntity`
        NSMutableArray *entities = @[].mutableCopy;
        [feedDicts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [entities addObject:[[LDEntity alloc] initWithDictionary:obj]];
        }];
        self.prototypeEntitiesFromJSON = entities;

        // 重返
        dispatch_async(dispatch_get_main_queue(), ^{
            !then ?: then();
        });
    });
}

//设置数据源
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.feedEntitySections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.feedEntitySections[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //LDCell为Main.storyboard中的cell控件
    LDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDCell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(LDCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
    if (indexPath.row % 2 == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    cell.entity = self.feedEntitySections[indexPath.section][indexPath.row];
}

//设置托付
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cellHeightCacheEnabled) {

        //LDCell为Main.storyboard中的cell控件

        return [tableView fd_heightForCellWithIdentifier:@"LDCell" cacheByIndexPath:indexPath configuration:^(LDCell *cell) {
            [self configureCell:cell atIndexPath:indexPath];
        }];
    } else {
        return [tableView fd_heightForCellWithIdentifier:@"LDCell" configuration:^(LDCell *cell) {
            [self configureCell:cell atIndexPath:indexPath];
        }];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray *mutableEntities = self.feedEntitySections[indexPath.section];
        [mutableEntities removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

@end

假设你想有自己的cell,你能够这么做:


#import "UITableView+FDTemplateLayoutCell.h"

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"reuse identifer" configuration:^(id cell) {
        // 配置此Cell(单元格)的数据, 同你在"-tableView:cellForRowAtIndexPath:"做了什么一样
        // 像这样:
        //    cell.entity = self.feedEntities[indexPath.row];
    }];
}

以下制作我们的最后一个功能就是下拉刷新操作

我们使用UITableViewController自带的UIRefreshControl

在LDViewController.m中加入此方法,而且与UIRefreshControl关联

//又一次载入数据

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.feedEntitySections removeAllObjects];
        [self.feedEntitySections addObject:self.prototypeEntitiesFromJSON.mutableCopy];
        [self.tableView reloadData];
        [sender endRefreshing];
    });

效果例如以下:

时间: 2024-08-11 09:43:17

使用FDTemplateLayout框架打造个性App的相关文章

【腾讯Bugly干货分享】从0到1打造直播 App

本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d42e7fd6ec467453bf58 作者:李智文 概要 分享内容: 互联网内容载体变迁历程,文字--图片/声音--视频--VR/AR----..从直播1.0秀场时代(YY),2.0游戏直播(斗鱼.虎牙.熊猫)到如今全民直播3.0泛生活娱乐时代(映客.花椒),国外直播app(Meerkat .Periscope),随着VA/AR/MR提出的沉浸式视听体验,直播4.0时

从0到1打造直播 App

转自http://dev.qq.com/topic/5811d42e7fd6ec467453bf58 概要 分享内容: 互联网内容载体变迁历程,文字——图片/声音——视频——VR/AR——……..从直播1.0秀场时代(YY),2.0游戏直播(斗鱼.虎牙.熊猫)到如今全民直播3.0泛生活娱乐时代(映客.花椒),国外直播app(Meerkat .Periscope),随着VA/AR/MR提出的沉浸式视听体验,直播4.0时代很快就能到来. 在这个全民娱乐的时代,直播已经火得不要不要的,各大公司都有自己

iOS利用HealthKit框架从健康app中获取步数信息

微信和QQ的每日步数最近十分火爆,我就想为自己写的项目中添加一个显示每日步数的功能,上网一搜好像并有相关的详细资料,自己动手丰衣足食. 统计步数信息并不需要我们自己去实现,iOS自带的健康app已经为我们统计好了步数数据 我们只要使用HealthKit框架从健康app中获取这个数据信息就可以了 这篇文章对HealthKit框架进行了简单的介绍:http://www.cocoachina.com/ios/20140915/9624.html 对HealthKit框架有了简单的了解后我们就可以开始了

爱加密出席2014可信云服务大会,协力打造移动App安全环境

2014年7月15日和7月16日,在工业和信息化部指导下,2014可信云服务大会在北京国际会议中心第一会议厅隆重召开.中国通信标准化协会秘书长杨泽民.中央国家机关政府采购中心主任王力达.工信部电信研究院院长曹淑敏.工信部总工程师张峰.工信部通信发展司副司长陈家春.财政部政府采购管理办公室主任王瑛等出席会议. 目前,云计算在国内已经进入快速发展阶段,用户对于云服务的接受程度也大幅提高,产业具有良好的发展前景.在该产业蓬勃发展的同时,云计算市场也存在鱼龙混杂,服务质量参次不齐,导致用户不信任,给云计

OCR移动端车牌识别SDK:打造优质APP;

我们为了达到:方便,快捷,精准提高前端人员工作效率.增强C端用户产品体验价值和目的. OCR移动端车牌识别-即通过手机/iPad/PDA手持终端,内置摄像头视频预览(扫一扫)模式,本地离线识别保存车牌号码. OCR移动端车牌识别(利用OCR光学字符分割识别技术)通过手机/iPad/PDA手持终端,内置摄像头视频预览(扫一扫)模式,将需要识别的车牌图像文件中的车牌号码,车牌颜色,车牌类型等自动采集处理并转换成可以编辑的文本格式,供软件开发商二次编辑调用,提供车牌识别SDK软件开发包. OCR移动端

使用appium框架测试安卓app时,获取toast弹框文字时,前一步千万不要加time.sleep等等待时间。

使用appium框架测试安卓app时,如果需要获取toast弹框的文案内容,那么再点击弹框按钮之前,一定记得千万不要加time.sleep()等待时间,否则有延迟,一直获取不到: 获取弹框的代码: message=self.driver.find_element_by_xpath("//*[contains(@text,'成功添加到购物车')]")   原文地址:https://www.cnblogs.com/zhouchuanlun/p/12687890.html

1.用互联网的产品思维打造一本app后端的书

刚刚接触app后端,是做完adidas中国的官方商城的时候,那时不清楚app后端应该怎么架构,只能摸着石头过河,网络上只有一些零散的资料,遇到问题,只能不断地搜索,思考,务必找到解决问题的方法. 在从事app后端的3年里,亲手打造了两款社交app,现在也在日pv过亿的云端平台里从事研发工作,慢慢地对app后端的架构有了一些体会. 把自己的工作笔记发表在CSDN博客专栏"app后端技术架构"发表后,收到了很多网友的反馈,后来为了方便交流,就创建了"app后端技术"qq

【轮子狂魔】抛弃IIS,打造个性的Web Server - WebAPI/Lua/MVC(附带源码)

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 引言 此篇是<[轮子狂魔]抛弃IIS,向天借个HttpListener - 基础篇(附带源码)>的续篇,也可以说是提高篇,如果你对HttpListener不甚了解的话,建议先看下基础篇. 这次玩的东西有点多了,大致分为如下几个方向: 1.支持

iOS知名第三方框架和流行APP们所用的第三方框架小结

网易新闻AppleReachabilityASIHTTPRequestEGOTableViewPullRefreshGTMNSString+HTMLMGTemplateEngineMPOAuthRegexKitLiteSDWebImageSSZipArchivewax GaragebandMurmurHashlibpngzlibSBJson (json-framework) iWork三套件MOKitBoost C++ LibraryprotobufOpenGL MathematicsSQLit