效果图同:http://blog.csdn.net/liu537192/article/details/43451707
核心代码:
//
// LiuJieViewController.m
// 01-应用管理
//
// Created by XinYou on 15-2-3.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
#import "LiuJieApp.h"
@interface LiuJieViewController ()
@property (nonatomic, strong) NSArray *apps;
@end
@implementation LiuJieViewController
- (NSArray *)apps{
if (_apps == nil) {
// 1,获取app.plist的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
// 2,加载数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *appArray = [NSMutableArray array];
// 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
for (NSDictionary *dict in dictArray) {
// 3.1.创建模型对象
// MJApp *app = [[MJApp alloc] initWithDict:dict];
LiuJieApp *app = [LiuJieApp appWithDict:dict];
// 3.2.添加模型对象到数组中
[appArray addObject:app];
}
// 4.赋值
_apps = appArray;
}
return _apps;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 总列数
int totalColumns = 3;
// 应用的尺寸(宽高)
CGFloat appViewW = 85;
CGFloat appViewH = 90;
// 应用之间的间距
CGFloat marginX = (self.view.frame.size.width - appViewW * totalColumns) / (totalColumns + 1);
CGFloat marginY = 15;
for (int index = 0; index < self.apps.count; index++) {
LiuJieApp *appInfo = self.apps[index];
// 1,整个应用对应一个UIView
UIView *appView = [[UIView alloc] init];
// [appView setBackgroundColor:[UIColor blueColor]];
// 计算行号和列号
int row = index / totalColumns;
int col = index % totalColumns;
CGFloat appViewX = marginX + col * (appViewW + marginX);
CGFloat appViewY = 30 + row * (appViewH + marginY);
appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
[self.view addSubview:appView];
// 2,应用图标
UIImageView *iconView = [[UIImageView alloc] init];
CGFloat iconViewW = 45;
CGFloat iconViewH = 45;
CGFloat iconViewX = (appViewW - iconViewW) / 2;
CGFloat iconViewY = 0;
// 设置位置和尺寸
iconView.frame = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
// [iconView setBackgroundColor:[UIColor redColor]];
iconView.image = [UIImage imageNamed:appInfo.icon];
[appView addSubview:iconView];
// 3,应用名字
UILabel *nameLabel = [[UILabel alloc] init];
CGFloat nameLabelW = appViewW;
CGFloat nameLabelH = 20;
CGFloat nameLabelX = 0;
CGFloat nameLabelY = iconViewY + iconViewH;
// 设置位置和尺寸
nameLabel.frame = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
// [nameLabel setBackgroundColor:[UIColor greenColor]];
// 设置文字
nameLabel.text = appInfo.name;
// 设置字体
nameLabel.font = [UIFont systemFontOfSize:13];
// 设置文字居中
nameLabel.textAlignment = NSTextAlignmentCenter;
[appView addSubview:nameLabel];
// 4,下载按钮
UIButton *downloadBtn = [[UIButton alloc] init];
CGFloat downloadBtnX = 12;
CGFloat downloadBtnY = nameLabelY + nameLabelH;
CGFloat downloadBtnW = appViewW - 2 *downloadBtnX;
CGFloat downloadBtnH = 20;
downloadBtn.frame = CGRectMake(downloadBtnX, downloadBtnY, downloadBtnW, downloadBtnH);
// [downloadBtn setBackgroundColor:[UIColor grayColor]];
// 设置默认的背景图片
UIImage *normalImage = [UIImage imageNamed:@"buttongreen"];
[downloadBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
// 设置高亮状态下得背景图片
UIImage *highlightedImage = [UIImage imageNamed:@"buttongreen_highlighted"];
[downloadBtn setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
// 设置按钮的文字
[downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
// 下面这种方式也可以设置按钮的文字,但是不推荐使用。
// 因为按钮是分状态的,用这种方式无法设置指定状态下的文字
// downloadBtn.titleLabel.text = @"下载";
// 设置按钮的文字字体
downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[appView addSubview:downloadBtn];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// LiuJieApp.h
// 01-应用管理
//
// Created by XinYou on 15-2-3.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface LiuJieApp : NSObject
/**
* 名称
*/
@property (nonatomic, copy) NSString *name;
/**
* 图标
*/
@property (nonatomic, copy) NSString *icon;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)appWithDict:(NSDictionary *)dict;
@end
//
// LiuJieApp.m
// 01-应用管理
//
// Created by XinYou on 15-2-3.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieApp.h"
@implementation LiuJieApp
- (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
这里就不提供源码下载地址了,只需参照核心代码对上一篇文章中的源码稍作修改即可