iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
一、plist文件和项目结构图
说明:这是一个嵌套模型的示例
二、代码示例:
YYcarsgroup.h文件代码:
//
// YYcarsgroup.h
// 07-汽车展示(高级)
//
// Created by apple on 14-5-28.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import <Foundation/Foundation.h> @interface YYcarsgroup : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,strong)NSArray *cars; -(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carsgroupWithDict:(NSDictionary *)dict;
@end
YYcarsgroup.m文件代码:
//
// YYcarsgroup.m
// 07-汽车展示(高级)
//
// Created by apple on 14-5-28.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYcarsgroup.h"
#import "YYcars.h" @implementation YYcarsgroup
-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self=[super init]) {
//嵌套的字典转模型
self.title=dict[@"title"]; //注意
NSArray *dictcars=dict[@"cars"];
//像下面这样写可以提高性能
NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:dictcars.count];
for (NSDictionary *dict in dictcars) {
YYcars *yycars=[[YYcars alloc]initWithDict:dict];
[arrayM addObject:yycars];
}
// 赋值存储模型的数组给属性
self.cars=arrayM;
}
return self;
} +(instancetype)carsgroupWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
@end
YYcars.h文件
//
// YYcars.h
// 07-汽车展示(高级)
//
// Created by apple on 14-5-28.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import <Foundation/Foundation.h> @interface YYcars : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon; -(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carsWithDict:(NSDictionary *)dict;
@end
YYcars.m文件
//
// YYcars.m
// 07-汽车展示(高级)
//
// Created by apple on 14-5-28.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYcars.h" @implementation YYcars -(instancetype)initWithDict:(NSDictionary *)dict
{
if (self=[super init]) {
self.name=dict[@"name"];
self.icon=dict[@"icon"];
}
return self;
}
+(instancetype)carsWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
@end
YYViewController.m文件
//
// YYViewController.m
// 07-汽车展示(高级)
//
// Created by apple on 14-5-28.
// Copyright (c) 2014年 itcase. All rights reserved.
// #import "YYViewController.h"
#import "YYcarsgroup.h"
#import "YYcars.h" @interface YYViewController ()<UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong) NSArray *car;
@end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad]; self.tableview.rowHeight=.f;
self.tableview.dataSource=self;
NSLog(@"%d",self.car.count);
}
#pragma mark- 实现懒加载
//1.从包中读取数据
//2.字典转模型
//3.返回cars
-(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) {
YYcarsgroup *carsgroup=[YYcarsgroup carsgroupWithDict:dict];
[carsarray addObject:carsgroup];
}
_car=[carsarray copy];
}
return _car;
} #pragma mark- 实现tableview的数据展示
//1.设置数据源,遵守协议
//2.返回组
//3.返回行
//4.每组每行对应的数据
//4.1去缓存中去取cell
//4.2若没有,则创建cell,并盖章
//4.3设置cell的数据
//4.4返回cell -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.car.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
YYcarsgroup *carsgroup=self.car[section];
return carsgroup.cars.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"car";
//4.1去缓存中去取cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//4.2若没有,则创建cell,并盖章
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//4.3设置cell的数据
//设置对应的组
YYcarsgroup *carsgroup=self.car[indexPath.section];
//设置对应的行
YYcars *yycars=carsgroup.cars[indexPath.row]; cell.imageView.image=[UIImage imageNamed:yycars.icon];
cell.textLabel.text=yycars.name;
//4.4返回cell
return cell;
} //设置每组的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
YYcarsgroup *carsgroup=self.car[section];
return carsgroup.title;
} //设置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//利用kvc取出所有的标题
NSArray *title=[self.car valueForKeyPath:@"title"];
return title;
} //隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end
实现效果:
三、注意点
1.设置索引
代码如下:
//设置索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//利用kvc取出所有的标题
NSArray *title=[self.car valueForKeyPath:@"title"];
return title;
}
2.cell的性能优化
代码如下:
static NSString *identifier=@"car";
//4.1去缓存中去取cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//4.2若没有,则创建cell,并盖章
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
请注意:cell内部数据处理的细节。(如何节省内存?)
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序的更多相关文章
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS开发UI篇—字典转模型
iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...
- iOS开发基础-Plist实现嵌套模型
一.plist文件结构图 说明: title 属性表示该 item 下汽车名字的首字母, cars 属性存放首字母为 title 的汽车, icon 属性存放图片的名称, name 属性存放汽车的名字 ...
- iOS开发UI篇—核心动画(关键帧动画)
转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...
- iOS开发UI篇—核心动画(基础动画)
转自:http://www.cnblogs.com/wendingding/p/3801157.html 文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(基础动画) iOS ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
- iOS开发UI篇—简单介绍静态单元格的使用
iOS开发UI篇—简单介绍静态单元格的使用 一.实现效果与说明 说明:观察上面的展示效果,可以发现整个界面是由一个tableview来展示的,上面的数据都是固定的,且几乎不会改变. 要完成上面的效果, ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—xib的简单使用
iOS开发UI篇—xib的简单使用 一.简单介绍 xib和storyboard的比较,一个轻量级一个重量级. 共同点: 都用来描述软件界面 都用Interface Builder工具来编辑 不同点: ...
随机推荐
- python 清楚数组重复字符串元素
l1 = ['bb','c','d','bb','c','a','a'] l2 = {}.fromkeys(l1).keys() print (l2)
- 指定YUM安装包的体系结构或版本
在单一体系结构下同时安装32位和64位包echo ‘multilib_policy=all’ >> /etc/yum.conf 指定体系结构 查看当前系统体系结构[root@oracle ...
- __block 与 __weak的区别理解
Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...
- Centos7下Rinetd安装与应用
Linux下做地址NAT有很多种方法.比如haproxy.nginx的4层代理,linux自带的iptables等都能实现.haproxy.nginx就不说了,配置相对简单:iptables配置复杂, ...
- Codeforces 749D:Leaving Auction(set+二分)
http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...
- win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn
python包下载网址 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 1.已经安装python2.7.12,查看scripts里是否有pip.2.7.9版本以上 ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- 从yum源下载安装包及依赖包
局域网内所有linux都用yum从外网源安装软件有点浪费,尤其遇到下载慢的情况: 所以考虑下载后传到其他机器安装,还可以保证版本一致(创建一个本地仓库更好,这个后面研究了再记录): 首先安装yum工具 ...
- 9-this
第九课 this 一.this基本概念 this是Javascript语言的一个关键字.在JavaScript中,this是动态绑定,或称为运行期绑定的.在不同的情况下,this指向各不相同.但是有一 ...
- [问题2014A05] 复旦高等代数 I(14级)每周一题(第七教学周)
[问题2014A05] (1) 设 \(x_1,x_2\cdots,x_n,x\) 都是未定元, \(s_k=x_1^k+x_2^k+\cdots+x_n^k\,(k\geq 1)\), \(s_0 ...