资料:汽车图片 和plist
文件:两个模型
// // Car.h // 汽车品牌 // // Created by YaguangZhu on 15/8/16. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <Foundation/Foundation.h> @interface Car : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *icon; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)carWithDict:(NSDictionary *)dict; + (NSArray *)carsWithArray:(NSArray *)array; @end
// // Car.m // 汽车品牌 // // Created by YaguangZhu on 15/8/16. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import "Car.h" @implementation Car - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)carWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } + (NSArray *)carsWithArray:(NSArray *)array { //NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { [arrayM addObject:[self carWithDict:dict]]; } return arrayM; } @end
// // carGroup.m // 汽车品牌 // // Created by YaguangZhu on 15/8/16. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import "carGroup.h" #import "Car.h" @implementation carGroup - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; if (self) { //[self setValuesForKeysWithDictionary:dict]; [self setValue:dict[@"title"] forKey:@"title"]; self.cars = [Car carsWithArray:dict[@"cars"]]; } return self; } + (instancetype)carGroupWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } + (NSArray *)carGroups1 { NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { [arrayM addObject:[self carGroupWithDict:dict]]; } return arrayM; } @end
// // carGroup.h // 汽车品牌 // // Created by YaguangZhu on 15/8/16. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <Foundation/Foundation.h> @interface carGroup : NSObject @property(nonatomic,copy)NSString *title; @property(nonatomic,strong)NSArray *cars; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)carGroupWithDict:(NSDictionary *)dict; + (NSArray *)carGroups1; @end
// // ViewController.m // 汽车品牌 // // Created by YaguangZhu on 15/8/16. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import "ViewController.h" #import "carGroup.h" #import "Car.h" @interface ViewController ()<UITableViewDataSource> @property(nonatomic,strong)NSArray *carGroups1; @property(nonatomic,strong)UITableView *tableView; @end @implementation ViewController - (UITableView *)tableView { if (_tableView == nil) { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; [self.view addSubview:_tableView]; } return _tableView; } - (NSArray *)carGroups1 { if (_carGroups1 == nil) { _carGroups1 = [carGroup carGroups1]; } return _carGroups1; } - (void)viewDidLoad { [super viewDidLoad]; // NSLog(@"%@",self.carGroups1); [self tableView]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //数据源方法 //分组的总数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.carGroups1.count; } //每一组的总数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { carGroup *group = self.carGroups1[section]; return group.cars.count; } //单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell ==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } carGroup *group = self.carGroups1[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 titleForFooterInSection:(NSInteger)section { carGroup *group = self.carGroups1[section]; return group.title; } //右侧索引列表 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { /* NSMutableArray *arrayM = [NSMutableArray array]; for (carGroup *group in self.carGroups1) { [arrayM addObject:group.title]; } return arrayM;*/ //kvc 是cocoa 的大招 return [self.carGroups1 valueForKeyPath:@"title"]; } @end
时间: 2024-10-24 09:39:27