A.需求
1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model
2.使用KVC进行Model封装赋值
3.展示头字母标题
4.展示索引(使用KVC代替遍历取出所有索引值)
 
 
B.实现
1.Model嵌套
其实就是将另一个Model作为成员
 
.plist 文件结构
 
GroupCar中有存储Car的数组
 @property(nonatomic, strong) NSArray *cars;
 
封装Model的时候需要进行相应处理
CarGroup.m
         // 这里不能用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;
 
2.KVC
成员名使用和从dictionary取出来的键名一致,从而能用一个方法赋值所有成员属性,所见代码量
 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;
}
3.展示标题 setTitle
 /** 设置section头部标题 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
 
4.索引
 /** 索引 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
// 使用KVC取出数组,不用使用遍历封装
return [self.carGroups valueForKey:@"title"];
}
 
C.主要代码
 //
// 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索引的更多相关文章

  1. [iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示

    A.实现思路 1.拖入UITableView 2.拖曳.连线UITableView控件 3.Controller遵守UITalbeViewDataSource协议 4.设置UITableView的da ...

  2. [iOS基础控件 - 6.7.1] 微博展示 代码

      Controller: // // ViewController.m // Weibo // // Created by hellovoidworld on 14/12/4. // Copyrig ...

  3. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  4. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  5. [iOS基础控件 - 7.0] UIWebView

    A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的   2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...

  6. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

  7. [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo

    A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用   B.实现步骤 1.准备plist文件和国旗图片     2.创建模型 // // Flag.h // Co ...

  8. [iOS基础控件 - 6.9] 聊天界面Demo

    A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...

  9. iOS基础 - 控件属性

    一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...

随机推荐

  1. C# Winform应用程序占用内存较大解决方法整理

     微软的 .NET FRAMEWORK 现在可谓如火如荼了.但是,.NET 一直所为人诟病的就是“胃口太大”,狂吃内存,虽然微软声称 GC 的功能和智能化都很高,但是内存的回收问题,一直存在困扰,尤其 ...

  2. Bridging signals ZOJ 3627 POJ1631 HDU1950

    题意:给出一个从1-n的数字排列,求最长上升子序列长度. 直接说解法吧.新开一个数组d,d[i]表示的是能构成长度为i的上升子序列的在原序列中最后的那个数值.程序的主要过程:当循环到第i个的时候,如果 ...

  3. ActiveMQ之 TCP通讯机制

    ActiveMQ支持多种通讯协议TCP/UDP等,我们选取最常用的TCP来分析ActiveMQ的通讯机制.首先我们来明确一个概念:  客户(Client):消息的生产者.消费者对ActiveMQ来说都 ...

  4. Git教程(2)官方命令文档及常用命令表

    http://www.cnblogs.com/angeldevil/archive/2013/11/26/3238470.html 1,官方命令文档 http://www.git-scm.com/do ...

  5. Angularjs中input的指令和属性

    建议添加 novalidate属性(可无值)到form标签上,这样可以保证在表单不合法的情况下阻止浏览器继续提交数据. 可以给表单元素 input 添加 required 属性(可无值),让该表单成为 ...

  6. BZOJ_1027_[JSOI2007]_合金_(计算几何+Floyd求最小环)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1027 共三种金属,\(m\)种材料,给出每种材料中三种金属的占比. 给出\(n\)种合金的三种 ...

  7. POJ 2455 Secret Milking Machine (二分+无向图最大流)

    [题意]n个点的一个无向图,在保证存在T条从1到n的不重复路径(任意一条边都不能重复)的前提下,要使得这t条路上经过的最长路径最短. 之所以把"经过的最长路径最短"划个重点是因为前 ...

  8. Java [Leetcode 242]Valid Anagram

    题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  9. mysqldump使用

    mysqldump常用于MySQL数据库逻辑备份. 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump ...

  10. 【转】angular通过$http与服务器通信

    http://www.cooklife.cn/detail/54c5044ec93620284e964b58#View angular是一个前端框架,实现了可交互式的页面,但是对于一个web应用,页面 ...