##先看效果 ##具体实现 ###一、准备资料 图片资源和plist文件(源码中包含) ###二、创建模型api
//JCar.h文件 @interface JCar : NSObject /** * 图标 */ @property (nonatomic,copy) NSString *icon; /** * 名称 */ @property (nonatomic,copy) NSString *name; + (instancetype)carWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end //JCar.m文件 @implementation JCar + (instancetype)carWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict]; } - (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } @end
// JCarGroup.h文件 @interface JCarGroup : NSObject /** * 组标题 */ @property (nonatomic,copy) NSString *title; /** * 该组下全部的Car */ @property (nonatomic,strong) NSArray *cars; + (instancetype)groupWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end // JCarGroup.m文件 #import "JCarGroup.h" #import "JCar.h" @implementation JCarGroup + (instancetype)groupWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict]; } - (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { // 赋值组名称 self.title = dict[@"title"]; // 组中的汽车列表 NSArray *dictArray = dict[@"cars"]; NSMutableArray *carArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { JCar *car = [JCar carWithDict:dict]; [carArray addObject:car]; } self.cars = carArray; } return self; } @end
###三、实现效果数组
// // ViewController.m // 01-汽车品牌 // // Created by yshye on 15/12/15. // Copyright © 2015年 yshye. All rights reserved. // #import "ViewController.h" #import "JCar.h" #import "JCarGroup.h" @interface ViewController () <UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableview; /** * 车品牌数组 */ @property (nonatomic,strong) NSArray *carGroups; @end @implementation ViewController - (NSArray *)carGroups{ if (_carGroups == nil) { // 初始化 // 1.得到plist全路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"]; // 2.加载数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的全部字典转成模型对象,放在新的数组中 NSMutableArray *groupCar = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { // 3.1 建立模型对象 JCarGroup *group = [JCarGroup groupWithDict:dict]; // 3.2 添加模型对象到数组中 [groupCar addObject:group]; } // 4.赋值 _carGroups = groupCar; } return _carGroups; } - (void)viewDidLoad { [super viewDidLoad]; self.tableview.dataSource = self; self.tableview.delegate = self; } #pragma mark - 数据源方法 /** * 一共有多少数据,不写时,默认是一组 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carGroups.count; } /** * 第section组有多少行 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ JCarGroup *cg = self.carGroups[section]; NSArray * cars =cg.cars; return cars.count; } /** * 每一行显示怎么样的内容(call) */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.定义一个循环标示 static NSString *ID = @"car"; // 2.从缓存池中取出可循环利用的cell UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; // 3.缓存池中没有可循环利用的call if (cell == nil) { cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 4.设置数据 JCarGroup *cg = self.carGroups[indexPath.section]; JCar *car =cg.cars[indexPath.row]; cell.imageView.image = [UIImage imageNamed:car.icon]; cell.textLabel.text =car.name; return cell; } /** * 设置第section组显示的头部标题 */ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ JCarGroup *cg = self.carGroups[section]; return cg.title; } /** * 设置第section组显示的尾部标题 */ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ // JCarGroup *cg = self.carGroups[section]; return @""; } /** * 设置右侧导航索引 * * @param tableView <#tableView description#> * * @return <#return value description#> */ - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return [self.carGroups valueForKeyPath:@"title"]; } - (BOOL)prefersStatusBarHidden{ return YES; } #pragma mark - 代理方法 ///** // * 数据显示高度 // */ //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // return [super ta]; //} ///** // * 头部数据显示高度 // */ //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ // return 30; //} // ///** // * 尾部数据显示高度 // */ //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ // return 30; //} @end
##源码 01-汽车品牌.zip缓存