汽车品牌案例:
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
相关文章推荐
- 【案例】3个汽车品牌的社会化媒体营销案例
- 无刷新汽车品牌三级联动
- 全球顶级汽车品牌邮件营销设计精选
- 第六天课程(汽车品牌 英雄联盟项目)-琐碎知识
- 案例----某网站品牌列表的效果
- iOS-汽车品牌app
- jQuery-案例:试验品牌展示
- 【案例分享】如何写好一个品牌的品牌故事?
- 2013年1季度中国汽车品牌口碑研究报告 ——自主A级车
- 【iOS基础控件 6 】 汽车品牌展示 Model嵌套/KVC/TableView索引 <UITableView>
- 中国汽车品牌口碑榜之:--2013年第3季度福州微型车综合口碑排名
- 给买车的来点干货——汽车品牌分析
- 国内最全的汽车经销商 品牌企业各省分布情况
- 后设男生为某国际汽车品牌打造全新网络娱乐广告
- 【案例】舒邑:一个女装品牌的奇葩打法-@i黑马
- 2013年1季度中国汽车品牌口碑研究报告 ——自主A级车
- 2013年1季度中国汽车品牌口碑研究报告 ——自主A级车
- 中国汽车品牌口碑榜之:--2013年第3季度广州商用车综合口碑排名
- 国外优秀网站:世界30大汽车品牌网站设计欣赏
- UITableView小程序 汽车品牌