源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8443347
效果图:
核心代码:
//
// LiuJieCarGroup.h
// 01-汽车品牌
//
// Created by XinYou on 15-2-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface LiuJieCarGroup : NSObject
/**
* 头部标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 尾部描述
*/
@property (nonatomic, copy) NSString *desc;
/**
* 每组的汽车名集合
*/
@property (nonatomic, strong) NSArray *cars;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carGroupWithDict:(NSDictionary *)dict;
@end
//
// LiuJieCarGroup.m
// 01-汽车品牌
//
// Created by XinYou on 15-2-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieCarGroup.h"
@implementation LiuJieCarGroup
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.title = dict[@"title"];
self.desc = dict[@"desc"];
self.cars = dict[@"cars"];
}
return self;
}
+ (instancetype)carGroupWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
//
// LiuJieViewController.m
// 01-汽车品牌
//
// Created by XinYou on 15-2-12.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
#import "LiuJieCarGroup.h"
@interface LiuJieViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation LiuJieViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 设置UITableView的数据源,数据源需要实现UITableViewDataSource协议
self.tableView.dataSource = self;
}
/**
* 隐藏状态栏
*/
- (BOOL)prefersStatusBarHidden{
return YES;
}
- (NSArray *)carGroups{
if (_carGroups == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_simple.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tempCarGroups = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
LiuJieCarGroup *carGroup = [LiuJieCarGroup carGroupWithDict:dict];
[tempCarGroups addObject:carGroup];
}
_carGroups = tempCarGroups;
}
return _carGroups;
}
/**
* @return 一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.carGroups.count;
}
/**
* @return 每组数据各有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
LiuJieCarGroup *carGroup = self.carGroups[section];
NSInteger rows = carGroup.cars.count;
return rows;
}
/**
* @return 每行数据对应的UITableViewCell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] init];
// NSInteger section = indexPath.section;
// NSInteger row = indexPath.row;
LiuJieCarGroup *carGroup = self.carGroups[indexPath.section];
cell.textLabel.text = carGroup.cars[indexPath.row];
return cell;
}
/**
* @return 某一个组的顶部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
LiuJieCarGroup *carGroup = self.carGroups[section];
NSString *title = carGroup.title;
return title;
}
/**
* @return 某一个组的底部标题(描述)
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
LiuJieCarGroup *carGroup = self.carGroups[section];
return carGroup.desc;
}
@end