源码下载地址:http://download.csdn.net/detail/liu537192/8461813
效果图:
核心代码:
//
// LiuJieViewController.m
// 03-汽车品牌
//
// Created by Mac on 15-2-24.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
#import "LiuJieCarGroup.h"
#import "LiuJieCar.h"
@interface LiuJieViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
* 车品牌组数据
*/
@property (nonatomic,strong) NSArray *groups;
@end
@implementation LiuJieViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.dataSource = self;
}
- (NSArray *)groups{
if (_groups == nil) {
// 获取plist文件的全路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil];
// 加载数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
LiuJieCarGroup *group = [LiuJieCarGroup groupWithDict:dict];
[tempArray addObject:group];
}
_groups = tempArray;
}
return _groups;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.groups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
LiuJieCarGroup *group = self.groups[section];
return group.cars.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 定义一个循环标识
static NSString *ID = @"car";
// 从缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 缓存池中没有可循环利用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 设置数据
LiuJieCarGroup *group = self.groups[indexPath.section];
LiuJieCar *car = group.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{
LiuJieCarGroup *group = self.groups[section];
return group.title;
}
/**
* 返回右边索引条显示的字符串数据
*/
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
// valueForKey:只能取直接属性,不能取间接属性。
// [对象 valueForKey:@"title"]:获取对象的title属性的值
// [数组 valueForKey:@"title"]:无法获取数组中每个对象的title属性的值
// valueForKeyPath:既能取直接属性,又能取间接属性
// [对象 valueForKeyPath:@"title"]:获取对象的title属性的值
// [数组 valueForKeyPath:@"title"]:获取数组中每个对象的title属性的值
return [self.groups valueForKeyPath:@"title"];
}
// 隐藏状态栏
- (BOOL)prefersStatusBarHidden{
return YES;
}
@end
//
// LiuJieCarGroup.h
// 03-汽车品牌
//
// Created by Mac on 15-2-27.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface LiuJieCarGroup : NSObject
/**
* 每组的标题
*/
@property (nonatomic,copy) NSString *title;
/**
* 存放所有的汽车品牌(里面装的都是LiuJieCar的模型)
*/
@property (nonatomic,strong) NSArray *cars;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)groupWithDict:(NSDictionary *)dict;
@end
//
// LiuJieCarGroup.m
// 03-汽车品牌
//
// Created by Mac on 15-2-27.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieCarGroup.h"
#import "LiuJieCar.h"
@implementation LiuJieCarGroup
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
// 赋值标题
self.title = dict[@"title"];
//
NSArray *dictArray = dict[@"cars"];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
LiuJieCar *car = [LiuJieCar carWithDict:dict];
[tempArray addObject:car];
}
self.cars = tempArray;
}
return self;
}
+ (instancetype)groupWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
//
// LiuJieCar.h
// 03-汽车品牌
//
// Created by Mac on 15-2-27.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface LiuJieCar : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *name;
+ (instancetype)carWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
//
// LiuJieCar.m
// 03-汽车品牌
//
// Created by Mac on 15-2-27.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieCar.h"
@implementation LiuJieCar
+ (instancetype)carWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end