iOS开发基础-Plist实现嵌套模型
一、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实现嵌套模型的更多相关文章
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
- iOS开发基础-九宫格坐标(2)之模型
在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...
- iOS开发基础-九宫格坐标(6)
继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- iOS开发UI篇—字典转模型
iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...
- iOS开发基础-图片切换(3)之属性列表
延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...
- iOS开发——总结篇&IOS开发基础知识
IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...
- IOS开发基础环境搭建
一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包: ...
- iOS开发基础-九宫格坐标(5)
继续在iOS开发基础-九宫格坐标(4)的基础上进行优化. 一.改进思路 1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21.22行对控件属性的设置能否拿到视图类 WJQAp ...
随机推荐
- spring-mvc里的 <mvc:resources> 及静态资源访问
在进行Spring MVC的配置时,通常我们会配置一个dispatcher servlet用于处理对应的URL.配置如下: <servlet> <servlet-name>Sp ...
- 适用于app.config与web.config的ConfigUtil读写工具类
之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...
- Centos 7 Puppet之foreman介绍安装测试
一.简介 1.前言(引用一下网上的资料) 随着企业的 Linux 系统数量越来越多,管理问题便成为一个相对麻烦并需要急 迫解决的问题,这里有 2 个 Key Message:1)统一管控体系非常重要, ...
- Asp.net的DataGrid实现列冻结(C#)
# Asp.net的DataGrid实现列冻结(C#) 一.写在前面 列冻结即在拖动横向滚动条时,冻结的列会随着滚动条移动,使得该列不会因为拖动滚动条而被隐藏,呈现出仿佛冻结的效果.列冻结与表头冻结是 ...
- JS的arguments
arguments对象:当前函数内置的全局属性,表示当前函数的所有参数的集合可以用来检测函数实参的个数 使用环境:当函数的参数个数无法确定时,使用arguments 写一个函数输出arguments看 ...
- CSS3 font-face使用
在 CSS3 之前,web 设计师必须使用已在用户计算机上安装好的字体. 通过 CSS3,web 设计师可以使用他们喜欢的任意字体. 当您找到或购买到希望使用的字体时,可将该字体文件存放到 web 服 ...
- Dynamics 365 Customer Engagement V9 活动源功能报错的解决方法
微软动态CRM专家罗勇 ,回复300或者20190120可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 安装好Dynamic ...
- C# 正则表达式应用
正则表达式平时不常用,经常都是用的时候,临时抱佛脚,查文档,然后就是被各种坑之后,才会逐渐熟练. 在线正则表达式测试:http://tool.oschina.net/regex/ 在线JSON格式化: ...
- 关于Keychain
1.Keychain 浅析 2.iOS的密码管理系统 Keychain的介绍和使用 3.iOS开发中,唯一标识的解决方案之keyChain+UUID
- 常用Shell脚本命令(备忘)
此处纪录一些个人常用的Shell命令,留作复用 Linux 必备软件 Tmux 终端复用神器 zsh 无比强大Shell运行环境 oh my zsh 搭配zsh食用 uGet Linux下载工具 Do ...