#import <Foundation/Foundation.h>
@interface LHLCar : NSObject
/**
* 图标
*/
@property (nonatomic, copy)NSString *icon;
/**
* 车名
*/
@property (nonatomic, copy)NSString *name;
+ (instancetype)carWithDict:(NSDictionary *)dict;
@end
#import "LHLCar.h"
@implementation LHLCar
+ (instancetype)carWithDict:(NSDictionary *)dict{
LHLCar *car = [[self alloc] init];
// car.icon = dict[@"icon"];
// car.name = dict[@"name"];
[car setValuesForKeysWithDictionary:dict];
return car;
}
@end
#import <Foundation/Foundation.h>
@interface LHLCarGroups : NSObject
/**
* 车模型
*/
@property (nonatomic, strong) NSArray *cars;
/**
* 标题 ABCD...
*/
@property (nonatomic, copy) NSString *title;
+ (instancetype)groupsWithDict:(NSDictionary *)dict;
@end
#import "LHLCarGroups.h"
#import "LHLCar.h"
@implementation LHLCarGroups
+ (instancetype)groupsWithDict:(NSDictionary *)dict{
// 创建一个LHLCarGroups对象,用于接收dict传来的值
LHLCarGroups *groups = [[self alloc] init];
// 设置title
groups.title = dict[@"title"];
// 字典数组 -> 模型数组
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *carDict in dict[@"cars"]) {
[arrayM addObject:[LHLCar carWithDict:carDict]];
}
groups.cars = arrayM;
return groups;
}
@end
//
// ViewController.m
// 20-索引条
//
// Created by admin on 16/8/27.
// Copyright © 2016年 admin. All rights reserved.
//
#import "ViewController.h"
#import "LHLCar.h"
#import "LHLCarGroups.h"
@interface ViewController ()
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation ViewController
/**
* 懒加载
*/
- (NSArray *)carGroups{
if (_carGroups == nil) {
// 加载plist文件
NSArray *carDict = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil]];
// 字典模型 -> 数组模型
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *groupsDict in carDict) {
[arrayM addObject:[LHLCarGroups groupsWithDict:groupsDict]];
}
_carGroups = arrayM;
}
return _carGroups;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置索引条文字颜色
self.tableView.sectionIndexColor = [UIColor redColor];
// 设置索引条背景颜色
self.tableView.sectionIndexBackgroundColor = [UIColor grayColor];
}
/**
* 隐藏状态栏
*/
- (BOOL) prefersStatusBarHidden{
return YES;
}
#pragma mark - 数据源方法
/**
* 有多少个分组
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.carGroups.count;
}
/**
* 每一组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
LHLCarGroups *groups = self.carGroups[section];
return groups.cars.count;
}
/**
* 单元格
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"Car";
// 在缓存池中通过ID查找可重用的单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 如果没有找到 则创建一个cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 设置数据
// NSLog(@"%@",_carGroups);
LHLCarGroups *groups = self.carGroups[indexPath.section];
LHLCar *car = groups.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name;
return cell;
}
/**
* 设置表格的title
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
LHLCarGroups *groups = self.carGroups[section];
return groups.title;
}
/**
* 索引条
*/
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
// NSMutableArray *arrayM = [NSMutableArray array];
// for (LHLCarGroups *groups in self.carGroups) {
// [arrayM addObject:groups.title];
// }
// return arrayM;
// KVC取值
return [self.carGroups valueForKeyPath:@"title"];
}
@end