ios-应用管理 字典转模型

增加了一个app类 两个文件app.m app.h

app.m

//
//  app.m
//  应用管理
//
//  Created by YaguangZhu on 15/7/31.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "app.h"

@implementation app

-(app *)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        self.miaoshu = dict[@"miaoshu"];
        self.icon = dict[@"icon"];
    }
    return self;
}
+ (app *)appwithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
@end

app.h

//
//  app.h
//  应用管理
//
//  Created by YaguangZhu on 15/7/31.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface app : NSObject

@property (nonatomic,copy) NSString *miaoshu;
@property (nonatomic,copy) NSString *icon;

- (app *)initWithDict:(NSDictionary *)dict;
+ (app *)appwithDict:(NSDictionary *)dict;

@end
//
//  ViewController.m
//  应用管理
//
//  Created by YaguangZhu on 15/7/31.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"
#import "app.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *apps;


@end

@implementation ViewController
- (NSArray *)apps
{
    if (_apps == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *arrayModels = [NSMutableArray array];
        
        for (NSDictionary *dict in array) {
            app *model = [app appwithDict:dict];
            
            
           // model.miaoshu = dict[@"miaoshu"];
           // model.icon = dict[@"icon"];
            
            [arrayModels addObject:model];
        }
        _apps =arrayModels;
        
    }
    
    return  _apps;
   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    int colums =3;
    CGFloat viewWidth = self.view.frame.size.width;
    
    CGFloat appW = 75;
    CGFloat appH = 90;
    CGFloat marginTop = 30;
    CGFloat maginX = (viewWidth - colums*appW)/ (colums+1);
    CGFloat maginY = maginX;
    
     for (int i=0;i<self.apps.count;i++)//9 = self.apps.count
    {
        
        app *appModel = self.apps[i];
        UIView *appview = [[UIView alloc] init];
   // appview.backgroundColor = [UIColor redColor];
        int colIdx = i % colums;
        int rowIdx = i / colums;
        CGFloat appX = maginX+ colIdx *(appW +maginX);
        CGFloat appY = marginTop +rowIdx *(appH +maginY);
    appview.frame = CGRectMake(appX, appY, appW, appH);
    
    [self.view addSubview:appview];
        // 增加子控件
        UIImageView *imgViewIcon = [[UIImageView alloc]init];
       // imgViewIcon.backgroundColor = [UIColor blueColor];
        CGFloat iconW = 45;
        CGFloat iconH =45;
        CGFloat iconX = (appview.frame.size.width- iconW) *0.5;
        CGFloat iconY = 0;
        imgViewIcon.frame = CGRectMake(iconX, iconY, iconW, iconH);
        
        [appview addSubview:imgViewIcon];
        imgViewIcon.image = [UIImage imageNamed:appModel.icon];
        
        
        
        
        
        
        UILabel *lblName = [[UILabel alloc] init];
        //lblName.backgroundColor = [UIColor yellowColor];
        CGFloat nameW = appview.frame.size.width;
        CGFloat nameH = 20;
        CGFloat nameY= iconH;
        CGFloat nameX = 0;
        lblName.frame = CGRectMake(nameX, nameY, nameW, nameH);
        [appview addSubview:lblName];
        
        UIButton *btnDowload = [[UIButton alloc] init];
        btnDowload.backgroundColor = [UIColor blackColor];
        lblName.text = appModel.miaoshu;
        lblName.font = [UIFont systemFontOfSize:14];
        lblName.textAlignment = NSTextAlignmentCenter;
        
        
        
        
        CGFloat btnW =iconW;
        CGFloat btnH = 20;
        CGFloat btnX = iconX;
        //CGFloat btnY = nameY + nameH;
        CGFloat btnY = CGRectGetMaxY(lblName.frame);
        btnDowload.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [appview addSubview:btnDowload];
        [btnDowload setTitle:@"xia zai" forState:UIControlStateNormal];
        [btnDowload setTitle:@"finsh" forState:UIControlStateHighlighted];
        [btnDowload setBackgroundImage:[UIImage imageNamed:@"12"] forState:UIControlStateNormal];
        [btnDowload setBackgroundImage:[UIImage imageNamed:@"10"] forState:UIControlStateHighlighted];
        btnDowload.titleLabel.font = [UIFont systemFontOfSize:14];
        
        //按钮的单击事件
        [btnDowload addTarget:self action:@selector(btnDownClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
        
}
- (void)btnDownClick
{
    NSLog(@"ssss");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文地址:https://www.cnblogs.com/zhuyaguang/p/4693125.html