一、plist文件结构图

  说明: title 属性表示该 item 下汽车名字的首字母, cars 属性存放首字母为 title 的汽车, icon 属性存放图片的名称, name 属性存放汽车的名字。

二、代码实例

  新建一个继承自 NSObject 的类,命名为 WJQCars ,该类用于存放首字母相同的汽车。

 //  WJQCars.h
@interface WJQCars : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carsWithDict:(NSDictionary *)dict;
@end
 //  WJQCars.m
@implementation WJQCars - (instancetype)initWithDict:(NSDictionary *)dict {
self = [super init];
if (self) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
} + (instancetype)carsWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}

  新建一个继承自 NSObject 的类,并命名为 WJQCarsGroup ,该类用于存放不同的 WJQCars 。

 //  WJQCarsGroup.h
@interface WJQCarsGroup : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray *cars; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carsGroupWithDict:(NSDictionary *)dict;
@end

  在 WJQCarsGroup.m 中导入 WJQCars.h ,实现文件代码如下:

 //  WJQCarsGroup.m
- (instancetype)initWithDict:(NSDictionary *)dict {
self = [super init];
if (self) {
self.title = dict[@"title"];
NSArray *dictCars = dict[@"cars"];
// 有助于提高性能
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictCars.count];
for (NSDictionary *dict in dictCars) {
WJQCars *wcars = [[WJQCars alloc] initWithDict:dict];
[arrayM addObject:wcars];
}
self.cars = arrayM;
}
return self;
} + (instancetype)carsGroupWithDict:(NSDictionary *)dict {
return [[self alloc]initWithDict:dict];
}

  在 ViewController.m 中导入 WJQCarsGroup.h、WJQCars.h 。

 //  ViewController.m
@interface ViewController () <UITableViewDataSource>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *car;
@end

  将 tableView 属性与 Main.storyboard 中拖入的 Table View 视图建立连接。并让 ViewController 遵守 UITableViewDataSource 协议。

 //  ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = ;
self.tableView.dataSource = self;
NSLog(@"self.car.count = %d", self.car.count);
} // 从包中读取数据,实现字典转模型
- (NSArray *)car {
if (_car == nil) {
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
NSArray *arrayM = [NSArray arrayWithContentsOfFile:fullPath];
NSMutableArray *carsArray = [NSMutableArray array];
for (NSDictionary *dict in arrayM) {
WJQCarsGroup *carsGroup = [WJQCarsGroup carsGroupWithDict:dict];
[carsArray addObject:carsGroup];
}
_car = [carsArray copy];
}
return _car;
} #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.car.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
WJQCarsGroup *carsGroup = self.car[section];
return carsGroup.cars.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"car";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
WJQCarsGroup *carsGroup = self.car[indexPath.section];
WJQCars *cars = carsGroup.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:cars.icon];
cell.textLabel.text = cars.name;
return cell;
} // 设置每组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
WJQCarsGroup *carsGroup = self.car[section];
return carsGroup.title;
} // 设置首字母索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSArray *title = [self.car valueForKeyPath:@"title"];  // 使用KVC机制
return title;
}

参考博客:iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

实例代码:http://vdisk.weibo.com/s/DiY98QyXChyWt

iOS开发基础-Plist实现嵌套模型的更多相关文章

  1. iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

    iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...

  2. iOS开发基础-九宫格坐标(2)之模型

    在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...

  3. iOS开发基础-九宫格坐标(6)

    继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ...

  4. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  5. iOS开发UI篇—字典转模型

    iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...

  6. iOS开发基础-图片切换(3)之属性列表

    延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...

  7. iOS开发——总结篇&IOS开发基础知识

    IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...

  8. IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  9. iOS开发基础-九宫格坐标(5)

    继续在iOS开发基础-九宫格坐标(4)的基础上进行优化. 一.改进思路 1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21.22行对控件属性的设置能否拿到视图类 WJQAp ...

随机推荐

  1. 【Java入门提高篇】Day25 史上最详细的HashMap红黑树解析

    当当当当当当当,好久不见,最近又是换工作,又是换房子,忙的不可开交,断更了一小段时间,最重要的一篇迟迟出不来,每次都犹抱琵琶半遮面,想要把它用通俗易懂的方式进行说明,确实有一定的难度,可愁煞我也,但自 ...

  2. ubuntu开发项目不能执行热更新

    当项目开发到一定成熟度,项目基本上比较大(vue,angular,react,java,php等),在Ubuntu系统环境下,我们写了代码,但是不能想Windows一样执行热更新,这是因为Ubuntu ...

  3. javascript基础修炼(5)—Event Loop(Node.js)

    开发者的javascript造诣取决于对[动态]和[异步]这两个词的理解水平. 一. 一道考察异步知识的面试题 题目是这样的,要求写出下面代码的输出: setTimeout(() => { co ...

  4. DSAPI 添加删除程序到Windows启动

    使用DSAPI.dll中文件类里现成的功能,将使你可以快速高效地实现将程序加入Windows启动项或从启动项中删除. 简单也是非常地简单,但由于是比较独立的功能,所以单独发表为整个篇幅.  DSAPI ...

  5. BN算法

    批量归一化(BN: Batch Normalization) 1 BN训练 1)随机梯度下降法(SGD)对于训练深度网络简单高效,但是它有个毛病,就是需要我们人为的去选择参数,比如学习率.参数初始化. ...

  6. CSS深入理解流体特性和BFC特性下多栏自适应布局

    一.块状元素的流体特性与自适应布局 块状元素像放在容器中的水流一样,内容区域会随着margin, padding, border的出现自动填满剩余空间,这就是块状元素的流体特性. 来一个小实验: di ...

  7. float与double

    对数值类型的细节了解在大学里就是一带而过,自己始终也没好好看过.这是在csdn上看到的一篇文章,挺好的,记录下来. https://blog.csdn.net/Demon__Hunter/articl ...

  8. 42.Odoo产品分析 (四) – 工具板块(10) – 问卷(2)

    查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (四) – 工具板块(10) – 问卷(1) 4 页面 即问卷,点开一项查看:  可以看出,网页就是问卷本身的子目录,其中指明了该目录包括哪 ...

  9. Jquery 使用和Jquery选择器

    jQuery中的顶级对象($) jQuery 中最常用的对象即 $ 对象,要想使用 jQuery 的方法必须通过 $ 对象.只有将普通的 Dom 对象封装成 jQuery 对象,然后才能调用 jQue ...

  10. Linux中Apache服务器的简单配置

    配置Apache服务器: 1.安装Apache,可以参考Linux 中yum的配置来安装: yum install http* -y 2.修改SELinux: setenforce 0 查看: 3.防 ...