iOS之UI随笔-字典转模型以及双模型的构建
2015-10-03 15:43
363 查看
iOS开发过程中字典转模型是非常重要的一个知识点,在解析plist文件或者json文件时是必不可少的一步,下面用例子来介绍一下怎样在优化代码的同时解析出所需要的plist文件内容展现在tableView上面。以下摘自李明杰老师的代码段:
plist文件如下:
plist文件的最外面模型的构建.h文件
.m文件
第二个模型的.h文件
.m文件
效果图如下:
plist文件如下:
#import "MJViewController.h" #import "MJCarGroup.h" #import "MJCar.h" @interface MJViewController ()<UITableViewDataSource> /** * 车品牌组数据 */ @property (nonatomic, strong) NSArray *groups; @end @implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } //数组懒加载 - (NSArray *)groups { if (_groups == nil) { // 初始化 // 1.获得plist的全路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]; // 2.加载数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 NSMutableArray *groupArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { // 3.1.创建模型对象 MJCarGroup *group = [MJCarGroup groupWithDict:dict]; // 3.2.添加模型对象到数组中 [groupArray addObject:group]; } // 4.赋值 _groups = groupArray; } return _groups; } #pragma mark - 数据源方法 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.groups.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { MJCarGroup *group = self.groups[section]; return group.cars.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.定义一个循环标识 static NSString *ID = @"car"; // 2.从缓存池中取出可循环利用cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3.缓存池中没有可循环利用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 4.设置数据 MJCarGroup *group = self.groups[indexPath.section]; MJCar *car = group.cars[indexPath.row]; cell.imageView.image = [UIImage imageNamed:car.icon]; cell.textLabel.text = car.name; return cell; } /** * 第section组显示的头部标题 */ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { MJCarGroup *group = self.groups[section]; return group.title; } /** * 返回右边索引条显示的字符串数据 */ - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self.groups valueForKeyPath:@"title"]; } //影藏导航栏 - (BOOL)prefersStatusBarHidden { return YES; }
plist文件的最外面模型的构建.h文件
#import <Foundation/Foundation.h> @interface MJCarGroup : NSObject /** * 这组的标题 */ @property (nonatomic, copy) NSString *title; /** * 存放的所有的汽车品牌(里面装的都是MJCar模型) */ @property (nonatomic, strong) NSArray *cars; + (instancetype)groupWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end
.m文件
#import "MJCarGroup.h" #import "MJCar.h" @implementation MJCarGroup + (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) { MJCar *car = [MJCar carWithDict:dict]; [carArray addObject:car]; } self.cars = carArray; } return self; } @end
第二个模型的.h文件
#import <Foundation/Foundation.h> @interface MJCar : NSObject /** * 图标 */ @property (nonatomic, copy) NSString *icon; /** * 名称 */ @property (nonatomic, copy) NSString *name; + (instancetype)carWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end
.m文件
#import "MJCar.h" @implementation MJCar + (instancetype)carWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } @end
效果图如下:
相关文章推荐
- [Leetcode] - 96 Unique Binary Search Trees
- IOS初学笔记UI--UIView
- JPA主键生成策略
- HDU 1815, POJ 2749 Building roads(2-sat)
- UIScrollView与NSTimer计时器、分页指示器控件的使用
- UICollectionView的使用
- hdu5493 Queue 线段树
- note: please rebuild precompiled header ..... /UIKit-2DYE2I2D1B3GL.pcm 解决方案
- 69. Sqrt(x) (Divide-and-Conquer)
- java gui案例开发
- Longest Consecutive Sequence
- 109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)
- 108.Convert Sorted Array to Binary Search Tree(Array; Divide-and-Conquer, dfs)
- 条款20:宁以pass-by-reference-to-const替换pass-by-value
- servlet中response和request的字符编码问题
- MAVEN ERROR: unable to find valid certification path to requested target 解决办法
- SuiShenJi项目_dialog to Activity动画
- SuiShenJi项目_按钮等背景样式
- SuiShenJi项目_Activity跳转、传值
- SuiShenJi项目_计算器逻辑