//
// ViewController.m
// 05展示 多汽车数据
//
// Created by Itcast.GuangZhou on 15/9/19.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "ViewController.h"
#import "CZCar.h"
#import "CZCarGroup.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//模型数组
@property(nonatomic,strong) NSArray *cars;
@end
@implementation ViewController
- (NSArray *)cars
{
if (_cars==nil) {
_cars=[CZCarGroup carGroups];
}
return _cars;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource=self;
self.tableView.delegate=self;
}
//确定组的数量,如果不实现默认为1组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.cars.count;
}
//确定组中一共有几行数据
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CZCarGroup *cargroup=self.cars[section];
return cargroup.cars.count;
}
//创建每一行显示的cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *ID=@"car";
//1.创建cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//获取 模型
//先取出组
CZCarGroup *carGroup=self.cars[indexPath.section];
//再取出这个组中的某个car模型
CZCar *car=carGroup.cars[indexPath.row];
//2.为cell赋值
cell.imageView.image=[UIImage imageNamed:car.icon];
cell.textLabel.text=car.name;
//返回
return cell;
}
//设置每一个组的头部标题
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//先取出组
CZCarGroup *carGroup=self.cars[section];
return carGroup.title;
}
//显示组导航
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *arr=[NSMutableArray array];
for (CZCarGroup *group in self.cars) {
[arr addObject:group.title];
}
return arr;
}
@end
<pre name="code" class="objc">//
// CZCar.h
// 05展示 多汽车数据
//
// Created by Itcast.GuangZhou on 15/9/19.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CZCar : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *name;
- (instancetype) initWithDic:(NSDictionary *)dic;
+ (instancetype) carWithDic:(NSDictionary *)dic;
//接收一个字典数组,返回一个模型数组
+ (NSArray *) cars:(NSArray *)dicArr;
@end
<pre name="code" class="objc">//
// CZCar.m
// 05展示 多汽车数据
//
// Created by Itcast.GuangZhou on 15/9/19.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "CZCar.h"
@implementation CZCar
- (instancetype)initWithDic:(NSDictionary *)dic
{
if(self=[super init])
{
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
+ (instancetype)carWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
//通过字典数组转换模型数组
+ (NSArray *)cars:(NSArray *)dicArr
{
NSMutableArray *desArr=[NSMutableArray array];
for (NSDictionary *dic in dicArr) {
CZCar *car=[CZCar carWithDic:dic];
[desArr addObject:car];
}
return desArr;
}
@end
<pre name="code" class="objc">//
// CZCarGroup.h
// 05展示 多汽车数据
//
// Created by Itcast.GuangZhou on 15/9/19.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CZCarGroup : NSObject
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) NSArray *cars;
- (instancetype) initWithDic:(NSDictionary *)dic;
+ (instancetype) carGroupWithDic:(NSDictionary *)dic;
+ (NSArray *) carGroups;
@end
<pre name="code" class="objc">//
// CZCarGroup.m
// 05展示 多汽车数据
//
// Created by Itcast.GuangZhou on 15/9/19.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "CZCarGroup.h"
#import "CZCar.h"
@implementation CZCarGroup
- (instancetype)initWithDic:(NSDictionary *)dic
{
if (self=[super init]) {
self.title=dic[@"title"];
//dic[@"cars"]只是得到一个字典数组,里面的每一个item项还是一个字典,我们还需要对其再次进行转换
// NSMutableArray *arr=[NSMutableArray array];
// //self.cars=dic[@"cars"];
// for (NSDictionary *subDic in dic[@"cars"]) {
// CZCar *car=[CZCar carWithDic:subDic];
// [arr addObject:car];
// }
// self.cars=arr;
self.cars=[CZCar cars:dic[@"cars"]];
}
return self;
}
+ (instancetype)carGroupWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
+ (NSArray *) carGroups
{
NSArray *sourceArrr=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
NSMutableArray *desArr=[NSMutableArray array];
for (NSDictionary *dic in sourceArrr) {
CZCarGroup *cg=[CZCarGroup carGroupWithDic:dic];
[desArr addObject:cg];
}
return desArr;
}
@end