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

九宫格代码--用了xib,plist加载和mvc思想

时间:2015-09-15 00:04:00      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

先看效果吧:cols =3时:

技术分享

cols =4时:

技术分享

部分代码如下:

 1 //
 2 //  ViewController.m
 3 //  0914-九宫格第3遍
 4 //
 5 //  Created by LongMa on 15/9/14.
 6 //  Copyright (c) 2015年 Dast. All rights reserved.
 7 //
 8 #import "ViewController.h"
 9 #import "CZApp.h"
10 #import "CZAppView.h"
11 
12 //宏定义app列数
13 #define cols 3
14 
15 @interface ViewController ()
16 @property (nonatomic, assign) NSArray* arrLazyLoad;
17 @end
18 
19 @implementation ViewController
20 //重写get方法
21 //  1.1.2懒加载model属性数组给属性arrLazyLoad
22 - (NSArray *)arrLazyLoad
23 {
24   if (nil == _arrLazyLoad)
25   {
26     _arrLazyLoad = [CZApp  arrWithModels];
27   }
28   return _arrLazyLoad;
29 }
30 
31 
32 - (void)viewDidLoad
33 {
34   [super viewDidLoad];
35   //  跟循环无关的参数写循环外面
36   CGFloat topMargin = 30;
37   
38   //  导入素材,plist文件
39   //  for循环创建app
40   for (int i = 0; i < self.arrLazyLoad.count; i++)
41   {
42     //  1.从model类获取model对象
43     //  1.1.1建立model类CZApp,按文件字典创建:属性个数跟字典键:数量、名字相同;
44     //  方法:类方法和对象方法返回对象;返回model作为成员的数组。
45     CZApp *app = [[CZApp alloc]init];
46     app = self.arrLazyLoad[i];
47     
48     //  2.从视图类获取视图对象
49     //  2.1创建视图类CZAppView(xibClass改为本视图类型,控件拖线到实现文件):同名类方法appView,
50     //  加载xib文件(先创建);app属性:重写set方法,为视图属性赋值
51     //  2.2.1建 视图对象
52     CZAppView *appView = [CZAppView appView];//默认坐标为(0,0),需要重设
53     
54     //  2.2.2添
55     [self.view addSubview: appView];
56     
57     //  2.2.3设 frame
58     CGFloat appViewW = appView.frame.size.width;
59     CGFloat appViewH = appView.frame.size.height;
60     //    横边距
61     CGFloat marginX = (self.view.frame.size.width - appViewW * cols) / (cols + 1);
62     CGFloat marginY = marginX;
63     
64     CGFloat appViewX = marginX + (i % cols) * (appViewW + marginX);
65     CGFloat appViewY = topMargin +(i / cols) * (appViewH + marginY);
66     
67     appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
68     
69     //  3.把model对象赋值给视图对象,为xib里图片设置图片,lbl设置名字
70     appView.app = app;
71   }
72 }
73 
74 @end
 1 //
 2 //  CZApp.h
 3 
 4 #import <Foundation/Foundation.h>
 5 
 6 @interface CZApp : NSObject
 7 
 8 //1.属性
 9 @property (nonatomic, copy) NSString *name;
10 @property (nonatomic, copy) NSString *icon;
11 //2.类方法
12 + (instancetype)appWithDic:(NSDictionary*)dic;
13 //3.构造对象方法
14 - (instancetype)initWithDic:(NSDictionary*)dic;
15 //4.返回对象数组的类方法
16 + (NSArray*)arrWithModels;
17 @end
 1 //
 2 //  CZApp.m
 3 
 4 #import "CZApp.h"
 5 
 6 @implementation CZApp
 7 
 8 //2.类方法
 9 + (instancetype)appWithDic:(NSDictionary*)dic
10 {
11   return [[self alloc]initWithDic:dic];
12 }
13 //3.构造对象方法
14 - (instancetype)initWithDic:(NSDictionary*)dic
15 {
16   if (self = [super init])
17   {
18     _name = dic[@"name"];
19     _icon = dic[@"icon"];
20   }
21   return self;
22 }
23 //4.返回对象数组的类方法
24 + (NSArray*)arrWithModels
25 {
26   NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]
27                                                    pathForResource:@"app.plist" ofType:nil]];
28   //  创建模型数组
29   NSMutableArray *arrM = [NSMutableArray array];
30   for (NSDictionary *dic in arr)
31   {
32     CZApp *model = [CZApp appWithDic:dic];
33     [arrM addObject:model];
34   }
35   return arrM;
36 }
37 @end
 1 //  2.1创建视图类CZAppView(xibClass改为本视图类型,控件拖线到是实现文件):同名类方法appView,
 2 //  加载xib文件(先创建);app属性:重写set方法,为视图属性赋值
 3 // CZAppView.h
 4 #import <UIKit/UIKit.h>
 5 @class CZApp;
 6 
 7 @interface CZAppView : UIView
 8 
 9 @property (nonatomic, strong) CZApp* app;
10 + (instancetype)appView;
11 
12 @end
 1 //
 2 //  CZAppView.m
 3 
 4 #import "CZAppView.h"
 5 #import "CZApp.h"//不引入的话_app点不出来属性
 6 
 7 @interface CZAppView ()
 8 
 9 @property (weak, nonatomic) IBOutlet UIImageView *iconView;
10 
11 @property (weak, nonatomic) IBOutlet UILabel *nameView;
12 @property (weak, nonatomic) IBOutlet UIButton *downloadView;
13 
14 @end
15 
16 @implementation CZAppView
17 
18 - (void)setApp:(CZApp *)app
19 {
20   _app = app;
21   self.iconView.image = [UIImage imageNamed:app.icon ];
22   self.nameView.text = app.name;
23   
24 }
25 
26 /**
27  *  取出xib里面的视图对象
28  *
29  *  @return <#return value description#>
30  */
31 + (instancetype)appView
32 {
33   return [[[NSBundle mainBundle] loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];
34 }
35 
36 
37 @end

技术分享

技术分享

技术分享

 

九宫格代码--用了xib,plist加载和mvc思想

标签:

原文地址:http://www.cnblogs.com/Dast1/p/4808903.html

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