1 //
2 // ViewController.m
3 // 汽车品牌
4 //
5 // Created by 刘羽 on 15/12/31.
6 // Copyright © 2015年 LX. All rights reserved.
7 //
8
9 #import "ViewController.h"
10 #import "Car.h"
11 #import "CarGroup.h"
12
13 @interface ViewController ()<UITableViewDataSource>
14
15 @property(strong,nonatomic)NSArray *carGroups;
16 @property(strong,nonatomic)UITableView *tableView;
17
18
19 @end
20
21 @implementation ViewController
22
23 -(UITableView *)tableView
24 {
25 if (_tableView == nil) {
26 _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
27 _tableView.dataSource = self;
28 [self.view addSubview:_tableView];
29 }
30 return _tableView;
31
32 }
33
34 -(NSArray *)carGroups
35 {
36 if (_carGroups == nil) {
37 //调用CarGroup类的 carGroups方法 返回模型数组
38 _carGroups = [CarGroup carGroups];
39 }
40 return _carGroups;
41 }
42
43
44
45
46 - (void)viewDidLoad {
47 [super viewDidLoad];
48 //调用tablelView添加到视图
49 [self tableView];
50 }
51
52 #pragma mark - 调用数据源方法
53
54 //返回分组总数
55 //NSInteger 不是对象类型 没有*
56 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
57 {
58 return self.carGroups.count;
59 }
60
61 //返回每一组的数量:这里每一组只算车的数量,不算标题title
62 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
63 { //获得每一个分组
64 CarGroup *group = self.carGroups[section];
65 //返回每一个分组中车数组的数量
66 return group.cars.count;
67 }
68
69 //设置单元格
70 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
71 {
72 //可重用标示符
73 static NSString *ID = @"Cell";
74 //让表格缓冲区查找可重用cell
75 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
76 //如果没有找到
77 if (cell == nil) {
78 //实例化cell
79 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
80
81 }
82 //设置cell的内容
83 //1>取出数据模型
84 CarGroup *group = self.carGroups[indexPath.section];
85 Car *car = group.cars[indexPath.row];
86 //2>设置数据
87 cell.imageView.image = [UIImage imageNamed:car.icon];
88 cell.textLabel.text = car.name;
89
90 return cell;
91 }
92
93 //设置标题
94 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
95 {
96 //找到group
97 CarGroup *group = self.carGroups[section];
98 return group.title;
99 }
100
101 //设置右侧索引列表
102 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
103 {
104 //索引数组中的内容跟分组无关
105 //索引数组中的下标对应的是分组的下标
106 // 返回self.carGroup中title的数组
107 // NSMutableArray *arrayM = [NSMutableArray array];
108 // for (HMCarGroup *group in self.carGroups) {
109 // [arrayM addObject:group.title];
110 // }
111 // return arrayM;
112
113 // KVC是cocoa的大招
114 // 用来间接获取或者修改对象属性的方式
115 // 使用KVC在获取数值时,如果指定对象不包含keyPath的"键名",会自动进入对象的内部查找
116 // 如果取值的对象是一个数组,同样返回一个数组
117 //一句代码就可以替代上面的四句代码
118 return [self.carGroups valueForKeyPath:@"title"];
119 }
120
121
122 @end
//
// CarGroup.h
// 汽车品牌
//
// Created by 刘羽 on 15/12/31.
// Copyright © 2015年 LX. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CarGroup : NSObject
//车首字母
@property (strong,nonatomic)NSString *title;
//车的数组,存放的是Car的模型数据
@property (strong,nonatomic)NSArray *cars;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carGroupWithDict:(NSDictionary *)dict;
+(NSArray *)carGroups;
@end
//
// CarGroup.m
// 汽车品牌
//
// Created by 刘羽 on 15/12/31.
// Copyright © 2015年 LX. All rights reserved.
//
#import "CarGroup.h"
#import "Car.h"
@implementation CarGroup
-(instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
//先将CarGroup中的title字典用kvc方法转为模型
[self setValue:dict[@"title"] forKey:@"title"];
//然后将CarGroup中的汽车数组转给Car类进行字典转模型处理并返回模型存入car数组
self.cars = [Car carsWithArray:dict[@"cars"]];
}
return self;
}
+(instancetype)carGroupWithDict:(NSDictionary *)dict
{//传递给上面的对象方法去处理
return [[self alloc]initWithDict:dict];
}
//加载plist 并调用以上方法转为数组模型
+(NSArray *)carGroups
{ //创建一个集合存储plist
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
//创建一个新的可变集合
NSMutableArray *arrayM = [NSMutableArray array];
//遍历array集合,将plist转为模型
for (NSDictionary *dict in array) {
[arrayM addObject:[self carGroupWithDict:dict]];
}
return arrayM;
}
//重写description方法,便于调试
-(NSString *)description
{
return [NSString stringWithFormat:@"<%@ : %p> {title: %@ , cars: %@}",self.class,self,self.title,self.cars];
}
@end
//
// Car.h
// 汽车品牌
//
// Created by 刘羽 on 15/12/31.
// Copyright © 2015年 LX. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property(strong,nonatomic)NSString *name;
@property(strong,nonatomic)NSString *icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carWithDict:(NSDictionary *)dict;
+(NSArray *)carsWithArray:(NSArray *)array;
@end
//
// Car.m
// 汽车品牌
//
// Created by 刘羽 on 15/12/31.
// Copyright © 2015年 LX. All rights reserved.
//
#import "Car.h"
@implementation Car
-(instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
+(NSArray *)carsWithArray:(NSArray *)array
{
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
//这里注意 方法不要写错 不要写成carsWithArray 这样变死循环了
[arrayM addObject:[self carWithDict:dict]];
}
return arrayM;
}
@end