21.小项目:应用市场 版本4.0

新建一个CZapp类,用对象来代替字典储存数据。  

------------- CZapp.h -------------

#import <Foundation/Foundation.h>


@interface CZApp : NSObject

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *icon;

@end

------------- CZapp.m -------------

#import "CZApp.h"


@implementation CZApp

@end

------------- ViewController.m -------------

#import "ViewController.h"

#import "CZApp.h"


@interface ViewController ()

@property (nonatomic,strong) NSArray *apps;

@end


@implementation ViewController


- (NSArray *)apps

{

    if(_apps == nil){

        NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];

        NSArray *array = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *arrayM = [NSMutableArray array];

        for (NSDictionary *dict in array)

        {

            CZApp *app = [[CZApp alloc] init];

            app.name = dict[@"name"];

            app.icon = dict[@"icon"];

            [arrayM addObject:app];

        }

        _apps = arrayM;

    }

    return _apps;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    CGFloat appW = 100;

    CGFloat appH = 100;

    int numOfCol = 3;

    CGFloat marginX = (self.view.frame.size.width - appW * numOfCol) / (numOfCol + 1);

    CGFloat marginY = 20;

    for (int index = 0; index < self.apps.count; index++)

    {

        UIView *appView = [[UIView alloc] init];

        int col = index % numOfCol;

        CGFloat appX = marginX + (appW + marginX) * col;

        int row = index / numOfCol;

        CGFloat appY = 20 + marginY + (appH + marginY) * row;

        appView.frame = CGRectMake(appX, appY,appW,appH);

        [self.view addSubview:appView];

        [self addSubviewsForAppView:appView atIndex:index];

    }

    

}


- (void) addSubviewsForAppView:(UIView *) appView atIndex:(int) index

{

    CGFloat appW = appView.frame.size.width;

    CGFloat appH = appView.frame.size.height;

    CZApp *app = self.apps[index];

    UIImageView *imageView = [[UIImageView alloc] init];

    CGFloat iconW = 50;

    CGFloat iconH = 50;

    CGFloat iconX = (appW - iconW) / 2;

    CGFloat iconY = 0;

    imageView.frame = CGRectMake(iconX,iconY , iconW, iconH);

    [appView addSubview:imageView];

    imageView.image = [UIImage imageNamed:app.icon];

    UILabel *nameLabel = [[UILabel alloc] init];

    CGFloat nameX = 0;

    CGFloat nameY = CGRectGetMaxY(imageView.frame);

    CGFloat nameW = appW;

    CGFloat nameH = 20;

    nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);

    [appView addSubview:nameLabel];

    nameLabel.text = app.name;

    nameLabel.textAlignment = NSTextAlignmentCenter;

    nameLabel.font = [UIFont systemFontOfSize:14];

    UIButton *downloadBtn = [[UIButton alloc] init];

    CGFloat downloadX = iconX;

    CGFloat downloadY = CGRectGetMaxY(nameLabel.frame);

    CGFloat downloadW = iconW;

    CGFloat downloadH = appH - downloadY;

    downloadBtn.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH);

    [appView addSubview:downloadBtn];

    [downloadBtn setTitle:@"下载" forState:UIControlStateNormal];

    [downloadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];

    [downloadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];

}


@end 

 

转载于:https://www.cnblogs.com/lixiang2015/p/4700301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值