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 ...
随机推荐
- Python MetaClass深入分析
python元类是比较难理解和使用的.但是在一些特定的场合使用MetaClass又非常的方便.本文本着先拿来用的精神,将对元类的概念作简要介绍,并通过深入分析一个元类的例子,来体会其功能,并能够在实际 ...
- Docker镜像构建的两种方式(六)--技术流ken
镜像构建介绍 在什么情况下我们需要自己构建镜像那? (1)当我们找不到现有的镜像,比如自己开发的应用程序 (2)需要在镜像中加入特定的功能 docker构建镜像有两种方式:docker commit命 ...
- Using rqt_console and roslaunch
Description: This tutorial introduces ROS using rqt_console and rqt_logger_level for debugging and r ...
- 大前端的自动化工厂(1)——Yeoman
一.Yeoman是什么 Yeoman是现代化前端项目的脚手架工具,用于生成包含指定框架结构的工程化目录结构.它是整个前端自动化工厂的第一站. 从个人使用者的角度来看,Yeoman的地位有些鸡肋,因为流 ...
- css字体图标的使用方法
提要:对于传统的一般用css雪碧(css sprite)来搞,目前大部分网站已经主要字体图标 ,利用font+css 或者font+html 来开发,今天总结了一下,记录之~ css sprite用背 ...
- netfilter及iptables基本概念
网络访问控制 网络访问控制可以简单理解为防火墙,常用的网络访问控制有:哪些IP可以访问服务器, 可以使用哪些协议,哪些接口,是否需要对数据包进行修改等. netfilter netfilter是通过i ...
- PHP-MySQL基本操作
PHP-MySQL基本操作 <?php // 1.防止页面中文乱码 header("content-type:text/html;charset=utf-8"); // 链接 ...
- 如何解决升级到Dynamics 365后有很多的Sandbox的WorkerProcess并导致异常?
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复254或者20170505可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
- openlayers3 实现点选的几种方式
WebGIS开发中,点击查询是最常用的一种查询方式,在ArcGIS api 中,这种查询叫IdentifyTask,主要作用是前台提交参数,交ArcServer查询分析返回.本文从开源框架的角度,从前 ...
- iOS----------弹窗动画
- (void)animationAlert:(UIView *)view { CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation ani ...