码迷,mamicode.com
首页 > 移动开发 > 详细

iOS应用管理(字典转模型)

时间:2017-09-01 17:51:45      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:strong   highlight   top   1.2   背景   属性   ntc   main   tom   

1. 新建appViewModel

1.1声明需要的属性

//NSString一般用copy来修饰

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *icon;

//字典转模型的方法

- (id)initWithDict:(NSDictionary *)dict;

 

1.2字典转模型

- (id)initWithDict:(NSDictionary *)dict{

    if(self = [super init]){

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

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

    }

    

    return self;

}

 

 

2.ViewController

#import "ViewController.h"

#import "appViewModel.h"

@interface ViewController ()

@property(nonatomic,strong)NSArray *apps;

@end

 

@implementation ViewController

 

- (NSArray *)apps{

    if(_apps == nil){

        //1-获取plist文件的路径

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

        //2-读取文件

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

        //3-字典转模型

        NSMutableArray *tempArr = [NSMutableArray array];

        for(NSDictionary *dic in dictArray){

            appViewModel *appModel = [[appViewModel alloc]initWithDict:dic];

            [tempArr addObject:appModel];

        }

        _apps = tempArr;

    }

    return _apps;

}

 

- (void)viewDidLoad{

    [super viewDidLoad];

    //1-设置frame的相关属性

    CGFloat appViewW = 100;

    CGFloat appViewH = 120;

    CGFloat spaceX = 20;

    CGFloat spaceY = 30;

    CGFloat topMargin = 30;

    CGFloat leftmargin = (self.view.frame.size.width - 3 * appViewW - 2 * spaceX) * 0.5;

    //2-创建格子

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

        //2.1-创建一个格子视图

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

        int row = i / 3;//行号

        int col = i % 3;//列号

        CGFloat appViewX = leftmargin + (appViewW + spaceX) * col;

        CGFloat appViewY = topMargin + (appViewH + spaceY) * row;

        //2.2设置frame

        appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);

        //2.3添加

        [self.view addSubview:appView];

        appViewModel *appViewModel = self.apps[i];

        //2.4添加图片

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

        CGFloat headW = 60;

        CGFloat headX = (appViewW - headW) *0.5;

        CGFloat headY = 0;

        CGFloat headH = 60;

        head.image = [UIImage imageNamed:  appViewModel.icon ];

        

        head.frame = CGRectMake(headX, headY, headW, headH);

        [appView addSubview:head];

        //        head.backgroundColor = [UIColor blueColor];

        

        //        2.5添加label

        //         2.5.1 创建一个文本标签

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

        CGFloat nameLabelX = 0;

        CGFloat nameLabelY = headY + headH;

        CGFloat nameLabelW = appViewW;

        CGFloat nameLabelH = 30;

        

        nameLabel.frame = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);

        [appView addSubview:nameLabel];

        //        nameLabel.backgroundColor = [UIColor grayColor];

        nameLabel.text = appViewModel.name;

        //       2.5.2设置文字大小

        nameLabel.font = [UIFont systemFontOfSize:13];

        //        2.5.3设置文字居中

        nameLabel.textAlignment = NSTextAlignmentCenter;

        

        //        2.6添加button

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

        CGFloat downLoadBtnX = 10;

        CGFloat downLoadBtnY = nameLabelY + nameLabelH + 10;

        CGFloat downLoadBtnW = appViewW - downLoadBtnX *2;

        CGFloat downLoadBtnH = 30;

        

        downLoadBtn.frame = CGRectMake(downLoadBtnX, downLoadBtnY, downLoadBtnW, downLoadBtnH);

        [appView addSubview:downLoadBtn];

        //        downLoadBtn.backgroundColor = [UIColor greenColor];//设置背景色

        

        [downLoadBtn setTitle:@"下载" forState:UIControlStateNormal];//设置文字

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

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

        //        downLoadBtn.

        //        按钮内部至少有两个控件,一个label,一个imageView,

        downLoadBtn.titleLabel.font = [UIFont systemFontOfSize:14];

        

    }

    

}

 

 

 

 

 

iOS应用管理(字典转模型)

标签:strong   highlight   top   1.2   背景   属性   ntc   main   tom   

原文地址:http://www.cnblogs.com/LiLihongqiang/p/7464154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!