您的位置:首页 > 其它

汽车品牌案例:

2015-01-09 09:28 190 查看
汽车品牌案例:

数据结构:
#import <Foundation/Foundation.h>

@interface Car : NSObject

@property(nonatomic, copy) NSString *icon;//图标
@property(nonatomic, copy) NSString *name;//名称

+ (instancetype)carWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;

@end

#import "Car.h"

@implementation Car

+ (instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init])
{
[self setValuesForKeysWithDictionary:dict];
}
return self;
}

@end

#import <Foundation/Foundation.h>

@interface CarGroup : NSObject

@property(nonatomic, copy) NSString *title;//标题
@property(nonatomic, strong) NSArray *cars;//存放所有汽车品牌

+ (instancetype)groupWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;

@end

#import "CarGroup.h"
#import "Car.h"

@implementation CarGroup

+ (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) {
Car *car = [Car carWithDict:dict];
[carArray addObject:car];
}
self.cars = carArray;
}

return self;
}

@end
--------------------------------------------------------------------------------------------------------

#import "ViewController.h"
#import "CarGroup.h"
#import "Car.h"

@interface ViewController () <UITableViewDataSource>

@property(nonatomic, strong) NSArray *groups;//所有车品牌数据

@end

@implementation ViewController

- (BOOL)prefersStatusBarHidden
{
return YES;
}

- (NSArray *)groups
{
if (_groups == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

NSMutableArray *groupsArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
CarGroup *group = [CarGroup groupWithDict:dict];
[groupsArray addObject:group];
}
_groups = groupsArray;
}
return _groups;
}

- (void)viewDidLoad {
[super viewDidLoad];
}

#pragma  mark - 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}

#pragma mark - 多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CarGroup *group = self.groups[section];
return group.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cargroup";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}

CarGroup *group = self.groups[indexPath.section];
Car *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
{
CarGroup *group = self.groups[section];
return group.title;
}

/**
返回索引栏的数据(右边索引条显示的ABCDE)
原理:这边对应的数组的索引对应左边显示的nstableview的索引
*/
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.groups valueForKeyPath:@"title"];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: