ios-day03-01(使用NSDictionary存储应用信息,九宫格形式显示应用信息)

本文介绍了一个名为LiuJieViewController的应用管理视图控制器的实现方法。该控制器从app.plist文件中加载应用列表,并根据定义的布局参数创建应用视图。文章提供了完整的Objective-C代码示例。

效果图:


核心代码:

//
//  LiuJieViewController.m
//  01-应用管理
//
//  Created by XinYou on 15-2-3.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieViewController.h"

@interface LiuJieViewController ()

@property (nonatomic, strong) NSArray *apps;

@end

@implementation LiuJieViewController

- (NSArray *)apps{

    if (_apps == nil) {
        // 获取app.plist的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        // 加载数据
        _apps = [NSArray arrayWithContentsOfFile:path];
    }
    
    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++) {
        
        NSDictionary *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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值