一、plist文件和项目结构图
说明:这是一个嵌套模型的示例
二、代码示例:
CarGroups.h文件代码:
#import <Foundation/Foundation.h>
@interface CarGroups : NSObject
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) NSArray *cars;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)groupsWithDict:(NSDictionary *)dict;
@end
CarGroups.m文件:
//
// CarGroups.m
// showCars
//
// Created by Toge on 6/4/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import "CarGroups.h"
#import "CarDes.h"
@implementation CarGroups
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
_title = dict[@"title"];
// cars也是字典类型的也要转换为模型
NSArray *carArray = dict[@"cars"];
NSMutableArray *temArray = [NSMutableArray array];
for (NSDictionary *carDict in carArray) {
CarDes *cars = [CarDes carWithDict:carDict];
[temArray addObject:cars];
}
_cars = temArray;
}
return self;
}
+ (instancetype)groupsWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
YYcars.h文件
1 // 2 // YYcars.h 3 // 07-汽车展示(高级) 4 // 5 // Created by apple on 14-5-28. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface YYcars : NSObject 12 @property(nonatomic,copy)NSString *name; 13 @property(nonatomic,copy)NSString *icon; 14 15 -(instancetype)initWithDict:(NSDictionary *)dict; 16 +(instancetype)carsWithDict:(NSDictionary *)dict; 17 @end
CarDes.h文件
//
// CarDes.h
// showCars
//
// Created by Toge on 6/4/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CarDes : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *name;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carWithDict:(NSDictionary *)dict;
@end
ViewController.m
//
// ViewController.m
// showCars
//
// Created by Toge on 6/4/15.
// Copyright (c) 2015 wxhl. All rights reserved.
//
#import "ViewController.h"
#import "CarGroups.h"
#import "CarDes.h"
@interface ViewController () <UITableViewDataSource> //遵守datasource协议
@end
@implementation ViewController
/**
*懒加载
*/
- (NSArray *)groups
{
if (_groups == nil) {
// 获取文件安装路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
NSArray *groupArray = [NSArray arrayWithContentsOfFile:path];
// 数组中的字典转成模型
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in groupArray) {
CarGroups *car = [CarGroups groupsWithDict:dict];
[tempArray addObject:car];
}
// groups里面的存储的是模型数组
_groups = tempArray;
}
return _groups;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建表视图对象
UITableView *tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 657) style:UITableViewStylePlain];
// 数据源代理设置
tabView.dataSource = self;
[self.view addSubview:tabView];
}
//返回有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
//返回每组数据有多少条
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 拿到对应的组
CarGroups *group = self.groups[section];
// 返回每组有多少条数据
return group.cars.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定义创建cel的标签
static NSString *ID = @"car";
// 在缓存池中查找对应标签的cel对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 判断cell是否为空,是空的要创建
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 赋值给对应的row
// 首先得到对应的车组
CarGroups *group = self.groups[indexPath.section];
CarDes *cars = group.cars[indexPath.row];
cell.textLabel.text = cars.name;
cell.imageView.image = [UIImage imageNamed:cars.icon];
return cell;
}
//返回组的名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CarGroups *group = self.groups[section];
return group.title;
}
/**
*返回右边索引拦现实的字符串数组
*/
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// kvc找不到键为title的值会自动到groups里面的元素找,找到对应的值,封装成数组返回,用valueForKey做不到
NSArray *array = [self.groups valueForKeyPath:@"title"];
return array;
}
@end
实现效果: