封装字典转模型 懒加载 MVC设计模式
来源:互联网 发布:linux 安装字体 编辑:程序博客网 时间:2024/04/29 08:18
代码详情:ViewController.m
//// ViewController.m#import "ViewController.h"#import "CZAppInfo.h"@interface ViewController ()//存储从plist中获取的数据@property (nonatomic, strong) NSArray *appInfos;@end@implementation ViewController//1 懒加载- (NSArray *)appInfos{ if (_appInfos == nil) { //1.1 bundle NSBundle *bundle = [NSBundle mainBundle]; //1.2 获取plist的路径 NSString *path = [bundle pathForResource:@"app" ofType:@"plist"]; //1.3 加载plist NSArray *dicArray = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *tmpArray = [NSMutableArray array]; //1.4 字典转换模型 for (NSDictionary *dic in dicArray) { CZAppInfo *appInfo = [[CZAppInfo alloc] init]; appInfo.name = dic[@"name"]; appInfo.icon = dic[@"icon"]; [tmpArray addObject:appInfo]; } _appInfos = tmpArray; } return _appInfos;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.// NSLog(@"%@",NSHomeDirectory()); //2 测试数据// NSLog(@"%@",self.appInfos); CGFloat subViewW = 100; CGFloat subViewH = 100; // 子view的横向间距 = (父view的宽度- 3 * 子view的宽度) / 4 CGFloat marginX = (self.view.frame.size.width - 3 * subViewW) / 4; // 子view的纵向间距 = 20 CGFloat marginY = 20; //3 动态生成9宫格的方块 for (int i = 0; i < self.appInfos.count; i++) { //动态生成view UIView *subView = [[UIView alloc] init]; [self.view addSubview:subView]; //计算frame // 当前子view的行号 = 当前遍历到得索引值 / 总列数 int row = i / 3;// 当前子view的列号 = 当前遍历到得索引值 % 总列数 int column = i % 3;// // 子view横坐标的公式 = 子view的横向间距 + 列号 * (子view的横向间距+ 子view的宽度) CGFloat subViewX = marginX + column * (marginX + subViewW);// 子view纵坐标的公式 = 20 + 行号 * (子view的纵向间距+ 子view的高度) CGFloat subViewY = 30 + row * (marginY + subViewH); subView.frame = CGRectMake(subViewX, subViewY, subViewW, subViewH);// subView.backgroundColor = [UIColor redColor]; //取得当前遍历到得数据 CZAppInfo *appInfo = self.appInfos[i]; // appInfo[@"name"]// appInfo[@"icon"]; [self displayAppInfo:appInfo subView:subView]; }}//方法的功能 生成三个子控件,显示应用信息//方法的返回值//方法的参数//方法内部的代码- (void)displayAppInfo:(CZAppInfo *)appInfo subView:(UIView *)subView{ CGFloat subViewW = subView.frame.size.width; //4 生成3个子控件 //4.1 iconView UIImageView *iconView = [[UIImageView alloc] init]; iconView.image = [UIImage imageNamed:appInfo.icon]; [subView addSubview:iconView]; //计算frame CGFloat iconW = 60; CGFloat iconH = 60; CGFloat iconY = 0; CGFloat iconX = (subViewW - iconW) / 2; iconView.frame = CGRectMake(iconX, iconY, iconW, iconH); //4.2 nameView UILabel *nameView = [[UILabel alloc] init]; [subView addSubview:nameView]; //计算frame CGFloat nameW = subViewW; CGFloat nameH = 20; CGFloat nameX = 0; CGFloat nameY = iconH; nameView.frame = CGRectMake(nameX, nameY, nameW, nameH); // nameView.text = appInfo.name; //文字大小 nameView.font = [UIFont systemFontOfSize:15]; //文字居中 nameView.textAlignment = NSTextAlignmentCenter; // // nameView.numberOfLines; //4.3 下载按钮 UIButton *downloadView = [UIButton buttonWithType:UIButtonTypeCustom]; [subView addSubview:downloadView]; //计算frame CGFloat downloadW = iconW; CGFloat downloadH = 20; CGFloat downloadX = iconX; //获得label的最大y值 CGFloat downloadY = CGRectGetMaxY(nameView.frame); downloadView.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH); [downloadView setTitle:@"下载" forState:UIControlStateNormal]; [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal]; [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted]; //设置按钮文字的大小 downloadView.titleLabel.font = [UIFont systemFontOfSize:15]; //给按钮注册事件 [downloadView addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside]; // downloadView.tag = i;}- (void)downloadClick{ NSLog(@"==");}@end
代码详情:CZAppInfo.h
//#import <Foundation/Foundation.h>@interface CZAppInfo : NSObject@property (nonatomic,copy) NSString *name;@property (nonatomic,copy) NSString *icon;@end
本文中自己已经传入plist文件 。。。。。
懒加载在用的时候一定要用self.array,因为只有这样才能调用他的get方法。。。否则就调用不到了
字典转模型的时候有三个数组,一个是全局的,存对象。。。在用到的时候要再赋给对象,然后用对象进行赋值@property (nonatomic, strong) NSArray *appInfos;
第二个是将字典取出来的数组,将网络请求的数组同个这个数组创建,然后通过for in方法把字典取出来,然后赋给对象。 NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
第三个数组是可变数组,在第二个数组中取出字典放在对象中,然后将对象一个一个存进去。。最后赋给第一个数组。NSMutableArray *tmpArray = [NSMutableArray array];
我们的数据应该封装起来,MVC格式就是将数据封装在外面。
经过封装字典后:
详情代码:CZAppInfo.h
//// CZAppInfo.h// A02-应用管理//// Created by Apple on 14/12/12.// Copyright (c) 2014年 itcast. All rights reserved.//#import <Foundation/Foundation.h>@interface CZAppInfo : NSObject@property (nonatomic,copy) NSString *name;@property (nonatomic,copy) NSString *icon;-(instancetype) initWithDict:(NSDictionary *)dict;+(instancetype) appInfoDict:(NSDictionary *)dict;+(NSArray *)appInfoList;@end
详情代码:CZAppInfo.m
//// CZAppInfo.m// A02-应用管理//// Created by Apple on 14/12/12.// Copyright (c) 2014年 itcast. All rights reserved.//#import "CZAppInfo.h"@implementation CZAppInfo-(instancetype) initWithDict:(NSDictionary *)dict{ if (self == [super init]) { self.name = dict[@"name"]; self.icon = dict[@"icon"]; } return self;}+(instancetype) appInfoDict:(NSDictionary *)dict{ return [[self alloc]initWithDict:dict];}+(NSArray *)appInfoList{ //1.1 bundle NSBundle *bundle = [NSBundle mainBundle]; //1.2 获取plist的路径 NSString *path = [bundle pathForResource:@"app" ofType:@"plist"]; //1.3 加载plist NSArray *dicArray = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *tmpArray = [NSMutableArray array]; //1.4 字典转换模型 for (NSDictionary *dic in dicArray) { CZAppInfo *appInfo = [CZAppInfo appInfoDict:dic]; [tmpArray addObject:appInfo]; } return tmpArray;}@end
详情代码:ViewController.m
//// ViewController.m// A02-应用管理//// Created by Apple on 14/12/12.// Copyright (c) 2014年 itcast. All rights reserved.//#import "ViewController.h"#import "CZAppInfo.h"@interface ViewController ()//存储从plist中获取的数据@property (nonatomic, strong) NSArray *appInfos;@end@implementation ViewController//1 懒加载- (NSArray *)appInfos{ if (_appInfos == nil) { _appInfos = [CZAppInfo appInfoList]; } return _appInfos;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.// NSLog(@"%@",NSHomeDirectory()); //2 测试数据// NSLog(@"%@",self.appInfos); CGFloat subViewW = 100; CGFloat subViewH = 100; // 子view的横向间距 = (父view的宽度- 3 * 子view的宽度) / 4 CGFloat marginX = (self.view.frame.size.width - 3 * subViewW) / 4; // 子view的纵向间距 = 20 CGFloat marginY = 20; //3 动态生成9宫格的方块 for (int i = 0; i < self.appInfos.count; i++) { //动态生成view UIView *subView = [[UIView alloc] init]; [self.view addSubview:subView]; //计算frame // 当前子view的行号 = 当前遍历到得索引值 / 总列数 int row = i / 3;// 当前子view的列号 = 当前遍历到得索引值 % 总列数 int column = i % 3;// // 子view横坐标的公式 = 子view的横向间距 + 列号 * (子view的横向间距+ 子view的宽度) CGFloat subViewX = marginX + column * (marginX + subViewW);// 子view纵坐标的公式 = 20 + 行号 * (子view的纵向间距+ 子view的高度) CGFloat subViewY = 30 + row * (marginY + subViewH); subView.frame = CGRectMake(subViewX, subViewY, subViewW, subViewH);// subView.backgroundColor = [UIColor redColor]; //取得当前遍历到得数据 CZAppInfo *appInfo = self.appInfos[i]; // appInfo[@"name"]// appInfo[@"icon"]; [self displayAppInfo:appInfo subView:subView]; }}//方法的功能 生成三个子控件,显示应用信息//方法的返回值//方法的参数//方法内部的代码- (void)displayAppInfo:(CZAppInfo *)appInfo subView:(UIView *)subView{ CGFloat subViewW = subView.frame.size.width; //4 生成3个子控件 //4.1 iconView UIImageView *iconView = [[UIImageView alloc] init]; iconView.image = [UIImage imageNamed:appInfo.icon]; [subView addSubview:iconView]; //计算frame CGFloat iconW = 60; CGFloat iconH = 60; CGFloat iconY = 0; CGFloat iconX = (subViewW - iconW) / 2; iconView.frame = CGRectMake(iconX, iconY, iconW, iconH); //4.2 nameView UILabel *nameView = [[UILabel alloc] init]; [subView addSubview:nameView]; //计算frame CGFloat nameW = subViewW; CGFloat nameH = 20; CGFloat nameX = 0; CGFloat nameY = iconH; nameView.frame = CGRectMake(nameX, nameY, nameW, nameH); // nameView.text = appInfo.name; //文字大小 nameView.font = [UIFont systemFontOfSize:15]; //文字居中 nameView.textAlignment = NSTextAlignmentCenter; // // nameView.numberOfLines; //4.3 下载按钮 UIButton *downloadView = [UIButton buttonWithType:UIButtonTypeCustom]; [subView addSubview:downloadView]; //计算frame CGFloat downloadW = iconW; CGFloat downloadH = 20; CGFloat downloadX = iconX; //获得label的最大y值 CGFloat downloadY = CGRectGetMaxY(nameView.frame); downloadView.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH); [downloadView setTitle:@"下载" forState:UIControlStateNormal]; [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal]; [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted]; //设置按钮文字的大小 downloadView.titleLabel.font = [UIFont systemFontOfSize:15]; //给按钮注册事件 [downloadView addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside]; // downloadView.tag = i;}- (void)downloadClick{ NSLog(@"==");}@end
4 0
- 封装字典转模型 懒加载 MVC设计模式
- 3.引入MVC设计模式,九宫格算法、字典转模型、自定义 view、xib
- 字典转模型&加载xib&加载plist
- 封装应用之字典转模型
- iOS模型设计---字典转模型
- MVC设计模式和JavaWeb三层模型
- (iOS-框架封装)iOS设计模式——MVC模式
- iOS开发之有趣的UI —— 数据转模型及MVC设计模式
- 关于三层架构,MVC模型,工厂设计模式三者
- Android GUI编程模型(MVC设计模式)
- Android GUI编程模型(MVC设计模式)
- 关于三层架构,MVC模型,工厂设计模式三者 .
- 关于三层架构,MVC模型,工厂设计模式三者 .
- Qt MVC设计模式:子类化抽象模型的方法
- MVC设计模式(转)
- iPhone开发之字典转模型——利用创建plist文件和懒加载
- IOS学习之——字典转模型 加载plist
- Plist文件、HUD、字典转模型、纯代码封装View、XIB封装View
- 【Android】Ant打包签名时中文alias怎么办
- JS parseFloat用法及精度问题
- TCP的拥塞控制
- ContentProvider
- jquery中attr和prop的区别
- 封装字典转模型 懒加载 MVC设计模式
- Linux命令之文件系统(十一)
- ubuntu sourceinsight 下smb挂载和gvfs的不同表现。
- Java并发编程:线程池的使用
- 关于信息技术
- 关于Android4.4前后的多媒体区分
- pb常用函数
- tomcat log 配置解决catalina.out文件过大问题
- JS延迟加载机制函数(Lazyload)