iOS开发UI基础—字典转模型
开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组来创建一个模型数组等等。
一、能完成功能的“问题代码”
1.从plist中加载的数据
2.实现的代码
![复制代码](http://img1.tuicool.com/jYN3ey.gif)
1 // 2 // LFViewController.m 3 // 03-应用管理 4 // 5 // Created by apple on 14-5-22. 6 // Copyright (c) 2014年 heima. All rights reserved. 7 // 8 9 #import "LFViewController.h" 10 11 @interface LFViewController () 12 @property (nonatomic, strong) NSArray *appList; 13 @end 14 15 @implementation LFViewController 16 17 - (NSArray *)appList 18 { 19 if (!_appList) { 20 21 // 1. 从mainBundle加载 22 NSBundle *bundle = [NSBundle mainBundle]; 23 NSString *path = [bundle pathForResource:@"app.plist" ofType:nil]; 24 _appList = [NSArray arrayWithContentsOfFile:path]; 25 26 NSLog(@"%@", _appList); 27 } 28 return _appList; 29 } 30 31 - (void)viewDidLoad 32 { 33 [super viewDidLoad]; 34 35 // 总共有3列 36 int totalCol = 3; 37 CGFloat viewW = 80; 38 CGFloat viewH = 90; 39 40 CGFloat marginX = (self.view.bounds.size.width - totalCol * viewW) / (totalCol + 1); 41 CGFloat marginY = 10; 42 CGFloat startY = 20; 43 44 for (int i = 0; i < self.appList.count; i++) { 45 46 int row = i / totalCol; 47 int col = i % totalCol; 48 49 CGFloat x = marginX + (viewW + marginX) * col; 50 CGFloat y = startY + marginY + (viewH + marginY) * row; 51 52 UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, y, viewW, viewH)]; 53 54 [self.view addSubview:appView]; 55 56 // 创建appView内部的细节 57 // 0> 读取数组中的字典 58 NSDictionary *dict = self.appList[i]; 59 60 // 1> UIImageView 61 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewW, 50)]; 62 imageView.image = [UIImage imageNamed:dict[@"icon"]]; 63 imageView.contentMode = UIViewContentModeScaleAspectFit; 64 [appView addSubview:imageView]; 65 66 // 2> UILabel 67 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.bounds.size.height, viewW, 20)]; 68 // 设置文字 69 label.text = dict[@"name"]; 70 label.font = [UIFont systemFontOfSize:12.0]; 71 label.textAlignment = NSTextAlignmentCenter; 72 73 [appView addSubview:label]; 74 75 // 3> UIButton 76 // UIButtonTypeCustom和[[UIButton alloc] init]是等价的 77 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 78 button.frame = CGRectMake(15, 70, viewW - 30, 20); 79 80 [button setTitle:@"下载" forState:UIControlStateNormal]; 81 // *** 不能使用如下代码直接设置title 82 // button.titleLabel.text = @"下载"; 83 // @property中readonly表示不允许修改对象的指针地址,但是可以修改对象的属性 84 button.titleLabel.font= [UIFont systemFontOfSize:14.0]; 85 86 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal]; 87 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted]; 88 89 [appView addSubview:button]; 90 } 91 } 92 93 @end
![复制代码](http://img1.tuicool.com/jYN3ey.gif)
3.实现效果
4.代码问题
在上述代码的第62,69行,我们是直接通过字典的键名获取plist中的数据信息,在viewController中需要直接和数据打交道,如果需要多次使用可能会因为不小心把键名写错,而程序并不报错。鉴于此,可以考虑把字典数据转换成一个模型,把数据封装到一个模型中去,让viewController不再直接和数据打交道,而是和模型交互。