本文共 5134 字,大约阅读时间需要 17 分钟。
//// ViewController.m// Copyright © 2016年 LongChuang. All rights reserved.//#import "ViewController.h"#import "CarData.h"#import "MyTableViewCell.h"@interface ViewController ()@property(nonatomic,weak)UITableView *tableview;@endNSString *ID = @"car";@implementation ViewController{ NSArray * _carData;}- (void)viewDidLoad { [super viewDidLoad]; // 接收数据 _carData = [self loadCarData]; // 创建tableView视图 [self createTableview]; // 注册 [self.tableview registerClass:[MyTableViewCell class] forCellReuseIdentifier:ID];}// 返回组数-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _carData.count;}// 返回每组的行数-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ CarData *cardata = _carData[section]; return cardata.cars.count;}// 设置每组的每行要显示的内容-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 使用IOS6之后的懒加载模式,需要提前设置tableView模板,即:注册 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath]; // 首先获取都第一次模型的数据,里面包含:数组cars 字符串title CarData *cardata = _carData[indexPath.section]; // 获取第二层模型,数组[下标]取到的是我们自己封装的第二层模型类型的数据 CarDataForCars *dict =cardata.cars[indexPath.row]; // 第二层中保存了车的名字和图标的名字 cell.textLabel.text = dict.name; cell.imageView.image =[UIImage imageNamed:dict.icon]; return cell;}// 创建tableview-(void)createTableview{ UITableView *tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; // 设置每行cell的高为64,默认高44 tableview.rowHeight = 64; // 添加到视图 [self.view addSubview:tableview]; // 给属性赋值 self.tableview = tableview; // 设置代理 self.tableview.dataSource = self;}// 设置主标题-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ CarData *cardata = _carData[section]; // 设置标题A B C D..... return cardata.title;}// 设置副标题-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"上面所有的��一律10元";}// 加载plist文件中的数据-(NSArray *)loadCarData{ // 读取路径 NSURL *url = [[NSBundle mainBundle] URLForResource:@"cars_total.plist" withExtension:nil]; // 加载数组 NSArray *arr = [NSArray arrayWithContentsOfURL:url]; // 解析数组双模型 NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count]; for (NSDictionary *dict in arr) { // 解析 cars和title 并保存到cardata对象属性中 CarData *cardata = [CarData createObj:dict]; // 将字典中已经赋值的数据传给数组 [arrM addObject:cardata]; } return arrM;}@end
//// CarData.h// 汽车品牌展示//// Copyright © 2016年 LongChuang. All rights reserved.//#import#import "CarDataForCars.h"@interface CarData : NSObject// 设置与plist文件中对应的key@property(nonatomic,copy) NSString *title;@property(nonatomic,strong)NSArray *cars;// 定义类方法,用于创建对象+(instancetype)createObj:(NSDictionary *)dict;-(instancetype)initWithDict:(NSDictionary *)dict;@end
//// CarData.m// 汽车品牌展示//// Created by Long on 16/8/9.// Copyright © 2016年 LongChuang. All rights reserved.//#import "CarData.h"#import "CarDataForCars.h"@implementation CarData// 在c视图中调用的此类方法创建实例对象+(instancetype)createObj:(NSDictionary *)dict{// id obj = [[self alloc]init];// [obj setValuesForKeysWithDictionary:dict];// return obj;// 单层模型的时候,我们常使用上面的方式赋值,多层模型中,如上赋值不能给cars赋值// 如下:// _title = dict[@"title"];// _cars = dict[@"cars"];//此处为一个未解析的数组,所以不能使用这种方式赋值 return [[self alloc] initWithDict:dict];}// 扩展初始化方法initWithXXXX-(instancetype)initWithDict:(NSDictionary *)dict{ self = [super init]; if (self) { // 这里使用直接赋值的方式 _title = dict[@"title"]; // 也可以快速赋值,但是给cars赋值的方法一定要在此方法下面,进行覆盖// [self setValuesForKeysWithDictionary:dict]; // 汽车字典数组-->转模型(CarDataForCars) NSArray *carList = dict[@"cars"]; // 创建容器用于保存数据 NSMutableArray *arrM = [NSMutableArray array]; for (NSDictionary *dict in carList) { CarDataForCars *carsModel = [CarDataForCars createObj:dict]; [arrM addObject:carsModel]; } _cars = arrM.copy; } return self;}@end
//// CarDataForCars.h// 汽车品牌展示// Copyright © 2016年 LongChuang. All rights reserved.//#import@class CarData;@interface CarDataForCars : NSObject@property(nonatomic,copy) NSString *icon;@property(nonatomic,copy) NSString *name;+(instancetype)createObj:(NSDictionary *) dict;-(instancetype)initWithCarDataForCars:(NSDictionary *)dict;@end
//// CarDataForCars.m// 汽车品牌展示// Copyright © 2016年 LongChuang. All rights reserved.//#import "CarDataForCars.h"#import "CarData.h"@implementation CarDataForCars-(instancetype)initWithCarDataForCars:(NSDictionary *)dict{ if (self = [super init]) { // _name = dict[@"name"]; // _icon = dict[@"icon"]; [self setValuesForKeysWithDictionary:dict]; } return self;}+(instancetype)createObj:(NSDictionary *) dict;{// 可以使用下面3行代码直接赋值,但如果再出现另一层模型,就还需要细分解析// id obj = [[self alloc]init];// [obj setValuesForKeysWithDictionary:dict];// return obj;// 所以直接使用扩展初始化方法的方式进行解析 return [[self alloc] initWithCarDataForCars:dict];}@end