博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS双层数据模型转换
阅读量:4086 次
发布时间:2019-05-25

本文共 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
你可能感兴趣的文章
GRPC学习笔记
查看>>
GIT命令
查看>>
【链表算法 leetcode】删除链表中等于给定值 val 的所有节点
查看>>
哈希表以及哈希算法的应用
查看>>
缓存相关问题及解决方式
查看>>
数据结构和算法笔记
查看>>
《大型分布式网站架构设计与实践》读书笔记
查看>>
多线程笔记
查看>>
RabbitMQ学习笔记
查看>>
Zookeeper笔记
查看>>
redis笔记
查看>>
mongoDB笔记
查看>>
在整数数组中找第二大的数
查看>>
秒杀系统设计要点
查看>>
Mybatis笔记
查看>>
分布式笔记
查看>>
Redis数据类型及其使用场景
查看>>
深入理解for update 和 lock in share mode
查看>>
他山之石
查看>>
on duplicate key update语句和insert ignore into语句
查看>>