[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 ...
随机推荐
- struts2 权限拦截器 拦截没有登陆的请求
假设有这样的登陆: ActionContext.getContext().getSession().put("UserMsg", userMsg); 则可以这样判断是否登陆: im ...
- H264码流解析及NALU
ffmpeg 从mp4上提取H264的nalu http://blog.csdn.net/gavinr/article/details/7183499 639 /* bitstream fil ...
- AD15高版软件卡不卡,问题解决大讨论
AD高版软件很卡(包括13 14 15版),这是我遇到过的问题,大家都遇到过的问题, 这里我分享一个解决办法:也请给位有什么好的方法也一起分享. 问题1卡:打开AD15软件, 按住鼠标中键 放大 或 ...
- HashMap源代码深入剖析
..
- Python之模块篇
简介 你已经学习了如何在你的程序中定义一次函数而重用代码.如果你想要在其他程序中重用很多函数,那么你该如何编写程序呢?你可能已经猜到了,答案是使用模块.模块基本上就是一个包含了所有你定义的函数和变量的 ...
- Django用户认证系统(一)User对象
User对象 User对象是认证系统的核心.用户对象通常用来代表网站的用户,并支持例如访问控制.注册用户.关联创建者和内容等.在Django认证框架中只有一个用户类,例如超级用户('superuser ...
- P140、面试题24:二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 测试用例: 1)功能测试(输入的后序遍历的序列 ...
- php简陋版实现微信公众号主动推送消息
推荐一个网站www.itziy.com csdn免积分下载器.pudn免积分下载器.51cto免积分下载器www.verypan.com 百度网盘搜索引擎www.94cto.com 编程相关视频教程. ...
- AIDL与stub
Stub翻译成中文是存根的意思,注意Stub对象是在被调用端进程,也就是服务端进程,至此,服务端aidl服务端得编码完成了. stub是为了方便client,service交互而生成出来的代码.A ...
- JavaScript DOM高级程序设计 7.向应用程序加入Ajax--我要坚持到底!
有时候,或许是因为理解能力,也或许是因为浮躁,看东西总是不入心,而且还老是想跳过本节,或者赶紧看完本节,这样的恶性循环,让我在即没有真正的学习到知识,又打击我的学习信心,还浪费了我很多事件,我想,当遇 ...