[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 ...
随机推荐
- EasyUI combotree值的设置 setValue
如果combotree的json数据如下: [ { "id":"2", "text":"wwwww", "st ...
- C++常用语法
unordered_map<int, Node*> mp; if (mp.find(key) == mp.end()) unordered_map<int, Node*>::i ...
- 网上图书商城项目学习笔记-035工具类之JdbcUtils及TxQueryRunner及C3P0配置
事务就是保证多个操作在同一个connection,TxQueryRunner通过JdbcUtils获取连接,而JdbcUtils通过ThreadLocal<Connection>确保了不同 ...
- Qt之QtSoap(访问WebService)
http://blog.csdn.net/u011012932/article/details/51673800
- Unable to open c
1. Unable to open c:\Cadence\PSD_14.2\tools\capture\allegro.cfg for reading. Please correct the abov ...
- 锋利的JQuery-Jquery中DOM操作
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 关于ckeditor添加的class都会被清除掉的问题
在源码中输入ul,并且带有class,然后点击源码,到可视化界面 结果显示为aaa,再点看源码,查看HTML源代码 解决方法: 添加配置 config.allowedContent = true 这个 ...
- Notepad++加上xml格式化的功能
工作中需要用代码创建一个XML文件,创建完发现XML内容都处在同一行,导致非常不容易查看清楚XML代码.于是习惯性地用 Notepad++查看,想用它来对XML代码格式化一下. 于是找到了TextFX ...
- bzoj3572
通过这题我知道了一个鬼故事,trunc(ln(128)/ln(2))=6……以后不敢轻易这么写了 好了言归正传,这题明显的构建虚树,但构建虚树后怎么树形dp呢? 由于虚树上的点不仅是议事会还有可能是议 ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求
具体设置如下: 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Setti ...