一、
*使用cars_total.plist
*模型嵌套模型
**注意:这里使用的是嵌套模型,所以不能直接使用KVC了。需要通过把字典转模型的代码封装到Group模型中。
二、
*实现右侧的索引栏
*通过实现数据源协议-(NSArray *)sectionIndexTitlesForTabView:(UITableView *)tableView
*点击右侧索引栏中的文字,会根据索引的顺序跳转到左侧对应的位置
*获取右侧group数组中每个对象的title值,并返回到一个NSArray中。[self.groups valueForKeyPath:@“title”];
三、注意:
一个包含对象的数组array,调用valueForKeyPath方法后会把指定键的所有值放到一个新的数组中。 例如:
NSArray *arr = [array valueForKeyPath:@“name”];
***因为array本身没有name属性,所以以上方法调用,会返回array这个数组中的每个元素对象的name属性的值,会把这些name放到一个新的NSArray中返回。******
编辑控制器的.h文件:
运行结果如下:
*使用cars_total.plist
*模型嵌套模型
**注意:这里使用的是嵌套模型,所以不能直接使用KVC了。需要通过把字典转模型的代码封装到Group模型中。
二、
*实现右侧的索引栏
*通过实现数据源协议-(NSArray *)sectionIndexTitlesForTabView:(UITableView *)tableView
*点击右侧索引栏中的文字,会根据索引的顺序跳转到左侧对应的位置
*获取右侧group数组中每个对象的title值,并返回到一个NSArray中。[self.groups valueForKeyPath:@“title”];
三、注意:
一个包含对象的数组array,调用valueForKeyPath方法后会把指定键的所有值放到一个新的数组中。 例如:
NSArray *arr = [array valueForKeyPath:@“name”];
***因为array本身没有name属性,所以以上方法调用,会返回array这个数组中的每个元素对象的name属性的值,会把这些name放到一个新的NSArray中返回。******
代码验证示例如下:plist文件如下:
新建工程,
编辑Car.h如下:
//
// CZCar.h
// 通过plist文件展现汽车品牌
//
// Created by apple on 15/9/1.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CZCar : NSObject
@property (nonatomic, strong) NSString * icon;
@property (nonatomic, strong) NSString *name;
-(instancetype)initWithDict:(NSDictionary *) dict;
+(instancetype ) carWithDict: (NSDictionary *) dict;
@end
编辑CZCar.m如下:
//
// CZCar.m
// 通过plist文件展现汽车品牌
//
// Created by apple on 15/9/1.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "CZCar.h"
@implementation CZCar
-(instancetype)initWithDict:(NSDictionary *) dict
{
if (self = [super init]) {
// self.name = dict[@"name"];
// self.icon = dict[@"icon"];
// 因为dict字典对象的数据元素都是基本类型,可以直接用KVC
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype ) carWithDict: (NSDictionary *) dict
{
return [[self alloc] initWithDict:dict];
}
@end
编辑CZGroup.h如下:
//
// CZGroup.h
// 通过plist文件展现汽车品牌
//
// Created by apple on 15/9/1.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CZCar.h"
@interface CZGroup : NSObject
@property (nonatomic, strong) NSArray * cars;
@property (nonatomic, strong) NSString *title;
-(instancetype) initWithDict:(NSDictionary *)dict;
+(instancetype) groupWithDict:(NSDictionary *)dict;
@end
编辑CZGroup.m如下:
//
// CZGroup.m
// 通过plist文件展现汽车品牌
//
// Created by apple on 15/9/1.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "CZGroup.h"
@implementation CZGroup
-(instancetype) initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
// self.name = dict[@"title"];
// self.cars = dict[@"cars"]; // 因为cars是个字典,要进行模型装换
[self setValuesForKeysWithDictionary:dict]; // 相当于给普通数据赋值
// 当有模型嵌套的时候需要手动把字典转为模型
// 创建一个用来保存模型的数组
NSMutableArray *arrayModels = [NSMutableArray array];
// 手动做一下模型转换
for(NSDictionary * item_dict in dict[@"cars"]) // cars也是一个字典数组
{ // 注意:声明的迭代项目不能和后面的集合名相同
CZCar *car = [CZCar carWithDict:item_dict];
[arrayModels addObject:car];
}
self.cars = arrayModels;
}
return self;
}
+(instancetype) groupWithDict:(NSDictionary *)dict
{
return [[self alloc ]initWithDict:dict];
}
@end
编辑控制器的.h文件:
//
// ViewController.h
// 通过plist文件展现汽车品牌
//
// Created by apple on 15/9/1.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CZGroup.h"
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray * groups; // 保存所有的数据
@property (nonatomic, strong) UITableView *tableView;
@end
编辑控制器的.m如下:
//
// ViewController.m
// 通过plist文件展现汽车品牌
//
// Created by apple on 15/9/1.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
// 重写groups的get方法实现懒加载
-(NSArray *)groups
{
if (_groups == nil) {
// 在app安装的根目录下获取指定plist文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
// 读取文件内容到一个数组
NSArray * arrayDicts = [NSArray arrayWithContentsOfFile:path];
// 创建一个保存模型对象的可变数组
NSMutableArray *arrayModels = [NSMutableArray array];
// 遍历字典数组,将字典转为对象模型并存入存放模型对象的可变数组
for(NSDictionary *dict in arrayDicts)
{
// 新建model对象
CZGroup *group = [CZGroup groupWithDict:dict];
[arrayModels addObject:group];
}
_groups = arrayModels;
}
return _groups;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//为UItableView组建设置属性
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
self.tableView.dataSource = self;
// 当每一行高度相同的情况下,可用以下属性设置
self.tableView.rowHeight = 60;
[self.view addSubview:self.tableView];
}
// 利用代理中的方法取消状态栏
-(BOOL) prefersStatusBarHidden
{
return YES;
}
// 分为多少组
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
// 每一组多行
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 获取模型数据
CZGroup * group = self.groups[section];
return group.cars.count;
}
// 每一组每一行是什么单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1 获取模型数据
// 根据组的索引获取组的模型
CZGroup *group = self.groups[indexPath.section];
// 根据当前行的索引,获取对应组的对应行的车
CZCar *car = group.cars[indexPath.row];
// 2 创建单元格
// 2.1 声明一个重用ID
static NSString *ID = @"Car_Cell";
// 2.2 根据重用ID去缓存池中获取对应的Cell
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
// 2.3 如果没有获取到,那么就创建一个具有重用ID的单元格
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 设置单元格内容
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name;
return cell;
}
// 添加数据源协议中的方法,实现添加表头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 获取组模型
CZGroup *group = self.groups[section];
return group.title;
}
// 利用代理中的方法添加右侧索引标题栏
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// NSMutableArray *arrayIndex = [NSMutableArray array];
// for(CZGroup *group in self.groups)
// {
// [arrayIndex addObject:group.title];
// }
// return arrayIndex;
//
return [self.groups valueForKeyPath:@"title"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行结果如下: