![[iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引 [iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
A.需求
1.使用汽车品牌名称头字母为一个Model,汽车品牌为一个Model,头字母Model嵌套品牌Model
2.使用KVC进行Model封装赋值
3.展示头字母标题
4.展示索引(使用KVC代替遍历取出所有索引值)
![[iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引 [iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMwLmNuYmxvZ3MuY29tL2Jsb2cvNjQ4NDczLzIwMTQxMi8wMjEyMDcyMDkzNjQ4NTkucG5n.png?w=700&webp=1)
B.实现
1.Model嵌套
其实就是将另一个Model作为成员
.plist 文件结构
![[iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引 [iOS基础控件 - 6.4] 汽车品牌展示 Model嵌套/KVC/TableView索引](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMwLmNuYmxvZ3MuY29tL2Jsb2cvNjQ4NDczLzIwMTQxMi8wMjEyMDcyMzA2MTYyNDMucG5n.png?w=700&webp=1)
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