//
// ViewController.m
// 07-应用管理
//
// Created by yibooo on 16/1/31.
// Copyright © 2016年 yibo_o. All rights reserved.
//
#import "ViewController.h"
#import "App.h"
@interface ViewController ()
/** 存放应用信息 */
@property (nonatomic, strong) NSArray *apps;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 添加应用信息
// 总列数
int totalColumns = 3;
//应用尺寸
CGFloat appW = 85;
CGFloat appH = 90;
// 间隙
CGFloat marginX = (self.view.frame.size.width - totalColumns * appW) / (totalColumns + 1);
CGFloat marginY = 20;
// 根据应用个数,创建对应的框框
for (int index = 0; index < self.apps.count; index++) {
// 创建一个框框
UIView *appView = [[UIView alloc] init];
// 框框的位置
CGFloat appX = marginX + (appW + marginX) * (index % totalColumns);
CGFloat appY = 30 + (marginY + appH) * (index / totalColumns);
appView.frame = CGRectMake(appX, appY, appW, appH);
[self.view addSubview:appView];
App *appInfo = self.apps[index];
// 添加内容的小控件
// 添加图片
UIImageView *iconView = [[UIImageView alloc] init];
CGFloat iconW = 45;
CGFloat iconH = 45;
CGFloat iconX = (appW - iconW) * 0.5;
CGFloat iconY = 0;
iconView.image = [UIImage imageNamed:appInfo.icon];
iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
[appView addSubview:iconView];
// 添加名字
UILabel *nameLabel = [[UILabel alloc] init];
CGFloat nameW = appW;
CGFloat nameH = 20;
CGFloat nameX = 0;
CGFloat nameY = iconY + iconH;
nameLabel.text = appInfo.name;
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.font = [UIFont systemFontOfSize:13];
nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
[appView addSubview:nameLabel];
// 添加下载按钮
UIButton *downloadBtn = [[UIButton alloc] init];
CGFloat downloadX = 10;
CGFloat downloadY = nameY + nameH;
CGFloat downloadW = appW - 2 * downloadX;
CGFloat downloadH = 20;
downloadBtn.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH);
[downloadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[downloadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateNormal];
[downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[appView addSubview:downloadBtn];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSArray *)apps {
if (_apps == nil) {
// 初始化
// 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 appWithDict:dict];
// 3.2.添加模型对象到数组中
[appArray addObject:app];
}
// 4.赋值
_apps = appArray;
}
return _apps;
}
@end
//
// app.h
// 07-应用管理
//
// Created by yibooo on 16/1/31.
// Copyright © 2016年 yibo_o. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface App : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict;
@end
//
// app.m
// 07-应用管理
//
// Created by yibooo on 16/1/31.
// Copyright © 2016年 yibo_o. All rights reserved.
//
#import "App.h"
@implementation App
- (instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+ (instancetype)appWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end