[iOS基础控件 - 4.2] APP列表 字典转模型Model
@interface App : NSObject /**
copy : NSString
strong: 一般对象
weak: UI控件
assign: 基本数据类型
*/ /**
名称
*/
@property(nonatomic, copy) NSString *name; /**
图标
*/
@property(nonatomic, copy) NSString *icon;
@end
#pragma mark 取得应用列表
- (NSArray *) apps {
if (nil == _apps) {
// 1.获得plist的全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; // 2.加载数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型,放到新数组中
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// 3.1创建模型对象
App *app = [[App alloc] init]; // 3.2 将字典的所有属性赋值给模型
app.name = dict[NAME_KEY];
app.icon = dict[ICON_KEY]; // 3.3 添加到app数组中
[appArray addObject:app];
} _apps = appArray;
} return _apps;
}
iconView.image = [UIImage imageNamed:appData.icon];
[appView addSubview:iconView]; // 2.设置APP名字
UILabel *nameLabel = [[UILabel alloc] init];
nameLabel.text = appData.name;
/**
自定义构造方法
通过字典来初始化模型对象
*/
- (id) initWithDictionary:(NSDictionary *) dictionary;
- (id) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
self.name = dictionary[NAME_KEY];
self.icon = dictionary[ICON_KEY];
}
return self;
}
// 3.1创建模型对象
App *app = [[App alloc] initWithDictionary:dict]; // 3.2 添加到app数组中
[appArray addObject:app];
+ (id) appWithDictionary:(NSDictionary *) dictionary {
// 使用self代表类名代替真实类名,防止子类调用出错
return [[self alloc] initWithDictionary:dictionary];
}


[iOS基础控件 - 4.2] APP列表 字典转模型Model的更多相关文章
- [iOS基础控件 - 4.3] APP列表 xib的使用
A.storyboard和xib 1.storyboard: 相对xib较重量级,控制整个应用的所有界面 2.xib: 轻量级,一般用来描述局部界面 B.使用 1.新建xib文件 New File ...
- [iOS基础控件 - 4.1] APP列表
需求 1.以N宫格的形式展示应用信息 2.APP信息包括图标.名字.下载按钮 3.使用尽可能少的代码,从plist读取app信息,计算每个app图标的位置尺寸信息 A.思路 1.UI布局:N宫 ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- [iOS基础控件 - 4.4] 进一步封装"APP列表”,初见MVC模式
A.从ViewController分离View 之前的代码中,View的数据加载逻辑放在了总的ViewController中,增加了耦合性,应该对控制器ViewController隐藏数据加载到Vie ...
- [iOS基础控件 - 6.9.3] QQ好友列表Demo TableView
A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组 code so ...
- [iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示
A.实现思路 1.拖入UITableView 2.拖曳.连线UITableView控件 3.Controller遵守UITalbeViewDataSource协议 4.设置UITableView的da ...
- [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储
A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改) 这个代码 ...
- iOS 基础控件(下)
上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...
- [iOS基础控件 - 5.4] 广告分页代码(UIScrollView制作)
A.概念 例子就是桌面的APP列表,当APP数量超过一个屏幕,自动进行分页 B.实现思路 1.创建一个UIScrollView,这里设置为宽度跟屏幕相同,高度1/4屏幕高度左右 2.使用代码在UI ...
随机推荐
- $.cookie 使用不了的问题定位过程
最近在项目中需要使用到jquery的cookie,按理说在html头中引入jquery-1.7.1.min.js和jquery.cookie.js,然后在js中就可以使用cookie函数了.像这样使用 ...
- ajax readyState的五种状态详解
通过ajax的readyState的值,我们可以知道当前的这个http请求处于什么状态.对于web的调试是比较重要的. readyState 状态说明: (0)未初始化 此阶段确认XMLHttpReq ...
- 179. Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- 验证Android用户输入日期
如何验证用户输入的日期是有效还是无效? private Pattern pattern; private Matcher matcher; private static final String DA ...
- Spring Framework 4.1.1
http://repo.spring.io/libs-release-local/org/springframework/spring/4.1.1.RELEASE/
- python调试 设置断点
1在所需要调试的地方加入如下代码: import pdb pdb.set_trace() 2调试代码常用命令: 实例请见参考文献: 1http://www.cnblogs.com/qi09/ar ...
- 统计学习方法笔记--EM算法--三硬币例子补充
本文,意在说明<统计学习方法>第九章EM算法的三硬币例子,公式(9.5-9.6如何而来) 下面是(公式9.5-9.8)的说明, 本人水平有限,怀着分享学习的态度发表此文,欢迎大家批评,交流 ...
- uva 11292 Dragon of Loowater (勇者斗恶龙)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- ubuntu12.04 make xconfig出错解决
xconfig是linux下X Window环境中用于配制的一个工具,和menuconfig相似,但用法更友好方便,用如下命令可以进入配制界面: make xconfig 因为在ubuntu系统中,编 ...
- MCC(移动国家码)和 MNC(移动网络码)
国际移动用户识别码(IMSI) international mobile subscriber identity 国际上为唯一识别一个移动用户所分配的号码. 从技术上讲,IMSI可以彻底解决国 ...