[iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引


@property(nonatomic, strong) NSArray *cars;
// 这里不能用KVC,封装car model再放到array中,不能直接存储array
NSArray *carsArray = dictionary[@"cars"];
NSMutableArray *mcarsArray = [NSMutableArray array];
for (NSDictionary *dict in carsArray) {
Car *car = [Car carWithDictionary:dict];
[mcarsArray addObject:car];
} self.cars = mcarsArray;
Car.m
- (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
// 当dictionary中的键名跟Model中的成员名一致的时候,可以使用KVC
[self setValuesForKeysWithDictionary:dictionary]; // self.icon = dictionary[@"icon"];
// self.name = dictionary[@"name"];
} return self;
}
/** 设置section头部标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
/** 索引 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// 使用KVC取出数组,不用使用遍历封装
return [self.carGroups valueForKey:@"title"];
}
//
// Car.h
// CarGroup
//
// Created by hellovoidworld on 14/12/2.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface Car : NSObject @property(nonatomic, copy) NSString *icon;
@property(nonatomic, copy) NSString *name; - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) carWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) car; @end
//
// Car.m
// CarGroup
//
// Created by hellovoidworld on 14/12/2.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "Car.h" @implementation Car - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
// 当dictionary中的键名跟Model中的成员名一致的时候,可以使用KVC
[self setValuesForKeysWithDictionary:dictionary]; // self.icon = dictionary[@"icon"];
// self.name = dictionary[@"name"];
} return self;
} + (instancetype) carWithDictionary:(NSDictionary *) dictionary {
return [[self alloc] initWithDictionary:dictionary];
} + (instancetype) car {
return [self carWithDictionary:nil];
} @end
//
// CarGroup.h
// CarGroup
//
// Created by hellovoidworld on 14/12/2.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface CarGroup : NSObject @property(nonatomic, strong) NSArray *cars;
@property(nonatomic, copy) NSString *title; - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) carGroupWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) carGroup; @end
//
// CarGroup.m
// CarGroup
//
// Created by hellovoidworld on 14/12/2.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "CarGroup.h"
#import "Car.h" @implementation CarGroup - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
self.title = dictionary[@"title"]; // 这里不能用KVC,封装car model再放到array中,不能直接存储array
NSArray *carsArray = dictionary[@"cars"];
NSMutableArray *mcarsArray = [NSMutableArray array];
for (NSDictionary *dict in carsArray) {
Car *car = [Car carWithDictionary:dict];
[mcarsArray addObject:car];
} self.cars = mcarsArray;
} return self;
} + (instancetype) carGroupWithDictionary:(NSDictionary *) dictionary {
return [[self alloc] initWithDictionary:dictionary];
} + (instancetype) carGroup {
return [self carGroupWithDictionary:nil];
} @end
//
// ViewController.m
// CarGroup
//
// Created by hellovoidworld on 14/12/1.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "CarGroup.h"
#import "Car.h" @interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView; // 所有的车品牌
@property(nonatomic, strong) NSArray *carGroups; @end
[iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引的更多相关文章
- [iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示
A.实现思路 1.拖入UITableView 2.拖曳.连线UITableView控件 3.Controller遵守UITalbeViewDataSource协议 4.设置UITableView的da ...
- [iOS基础控件 - 6.7.1] 微博展示 代码
Controller: // // ViewController.m // Weibo // // Created by hellovoidworld on 14/12/4. // Copyrig ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- iOS 基础控件(下)
上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...
- [iOS基础控件 - 7.0] UIWebView
A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的 2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用 B.实现步骤 1.准备plist文件和国旗图片 2.创建模型 // // Flag.h // Co ...
- [iOS基础控件 - 6.9] 聊天界面Demo
A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...
- iOS基础 - 控件属性
一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...
随机推荐
- 【mysql的设计与优化专题(2)】数据中设计中的范式与反范式
设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小.但是有些时候一昧的追求范式减少冗余,反而会降低数据读写 ...
- yii2的安装
yii2也是依赖于composer, 就像laravel, 所以先安装composer, 如果安装不上composer可以看laravel安装的文章. 安装好composer之后安装一个插件 comp ...
- 12232 - Exclusive-OR
12232 - Exclusive-OR 题目大意是可以设定一个点Xp=v,或者Xp^Xq=v,然后查询Xa^Xb^Xc...等于多少. 由于异或操作跟判连通性很类似,这里可以使用并查集来解决,对于X ...
- 从零开始定义自己的JavaScript框架(一)
来自:http://www.ituring.com.cn/article/48461 1.1 模块的定义 一个框架想要能支撑较大的应用,首先要考虑怎么做模块化.有了内核和模块加载系统,外围的模块就可以 ...
- 类似百度文库pdf2swf+flexpaper解决pdf在线阅读的效果
1:工具准备swftools.exe 下载http://www.swftools.org/download.html 安装至D盘SWFTools提供了一系列将各种文件转成swf的工具:font2swf ...
- 2013ACM省赛题目
地址就贴这一个吧 都在附近 当时回来也没做做 一伤心了 二是当时实在太弱了 先补两道DP E题的区间DP dp[i][j] 截止到i位置以字母j为结束的上升序列 正序 逆序各来一遍 再循环一遍保存一 ...
- Innodb parent table open时导致crash
case描述: innodb中,父表和子表通过foreign constraint进行关联, 因为在更新数据时需要check 外键constraint,如果父表被大量的子表reference, 那么在 ...
- Sencha touch Panel之间的跳转(如不使用TabPanel或者Carousel控件而产生跳转的动画效果)
常规的Sencha touch 应用都是"header content footer"结构,这样的结构无疑将使用TabPanel来实现,而且TabPanel肯定是card布局,这样 ...
- 【转】自定义iOS7导航栏背景,标题和返回按钮文字颜色 -- 不错不错!!
原文网址:http://blog.csdn.net/mad1989/article/details/41516743 在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更 ...
- 如何使用UDP进行跨网段广播
广播域首先我们来了解一下广播域的概念.广播域是网络中能接收任一台主机发出的广播帧的所有主机集合.也就是说,如果广播域内的其中一台主机发出一个广播帧,同一广播域内所有的其它主机都可以收到该广播帧.广播域 ...