源码下载
http://download.csdn.net/download/mashang123456789/9978766
//
// ViewController.m
// 索引条1
//
// Created by cdj on 17/9/13.
// Copyright © 2017年 ue. All rights reserved.
//
#import "ViewController.h"
#import "UECarGroup.h"
#import "UECar.h"
@interface ViewController ()
@property (nonatomic, strong)NSArray *carGroups;
@end
@implementation ViewController
-(NSArray *)carGroups{
if(!_carGroups){
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil] ];
NSMutableArray *temp = [NSMutableArray array];
for (NSDictionary *carGroupDict in dictArray) {
[temp addObject:[UECarGroup carGroupWithDict:carGroupDict]];
}
_carGroups = temp;
}
return _carGroups;
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置索引的文字颜色
self.tableView.sectionIndexColor = [UIColor redColor];
//设置索引的背景颜色
self.tableView.sectionIndexBackgroundColor = [UIColor yellowColor];
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
return YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.carGroups.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
UECarGroup *group = self.carGroups[section];
return group.cars.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"car";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
UECarGroup *group = self.carGroups[indexPath.section];
UECar *car = group.cars[indexPath.row];
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
UECarGroup *group = self.carGroups[section];
return group.title;
}
/**
* 添加索引
*
* @param tableView <#tableView description#>
*
* @return <#return value description#>
*/
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
/*
NSMutableArray *titles = [NSMutableArray array];
for (UECarGroup *group in self.carGroups) {
[titles addObject:group];
}
return titles;
*/
return [self.carGroups valueForKey:@"title"];
}
@end
//
// UECarGroup.h
// 索引条1
//
// Created by cdj on 17/9/14.
// Copyright © 2017年 ue. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UECarGroup : NSObject
@property(nonatomic, strong)NSMutableArray *cars;
@property(nonatomic, strong)NSString *title;
+(instancetype)carGroupWithDict:(NSDictionary *)dict;
@end
//
// UECarGroup.m
// 索引条1
//
// Created by cdj on 17/9/14.
// Copyright © 2017年 ue. All rights reserved.
//
#import "UECarGroup.h"
#import "UECar.h"
@implementation UECarGroup
+(instancetype)carGroupWithDict:(NSDictionary *)dict{
UECarGroup *group = [[self alloc] init];
group.title = dict[@"title"];
NSMutableArray *temp = [NSMutableArray array];
for (NSDictionary *carDict in dict[@"cars"]) {
[temp addObject:[UECar carWithDict:carDict]];
}
group.cars = temp;
return group;
}
@end
//
// UECar.h
// 索引条1
//
// Created by cdj on 17/9/14.
// Copyright © 2017年 ue. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UECar : NSObject
@property(nonatomic, copy)NSString *icon;
@property(nonatomic, copy)NSString *name;
+(instancetype)carWithDict:(NSDictionary *)dict;
@end
//
// UECar.m
// 索引条1
//
// Created by cdj on 17/9/14.
// Copyright © 2017年 ue. All rights reserved.
//
#import "UECar.h"
@implementation UECar
+(instancetype) carWithDict:(NSDictionary *)dict{
UECar *car = [[self alloc] init];
[car setValuesForKeysWithDictionary:dict];
return car;
}
@end