九宮格代碼--用了xib,plist載入和mvc思想

來源:互聯網
上載者:User

標籤:

先看效果吧: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 314 15 @interface ViewController ()16 @property (nonatomic, assign) NSArray* arrLazyLoad;17 @end18 19 @implementation ViewController20 //重寫get方法21 //  1.1.2懶載入model屬性數組給屬性arrLazyLoad22 - (NSArray *)arrLazyLoad23 {24   if (nil == _arrLazyLoad)25   {26     _arrLazyLoad = [CZApp  arrWithModels];27   }28   return _arrLazyLoad;29 }30 31 32 - (void)viewDidLoad33 {34   [super viewDidLoad];35   //  跟迴圈無關的參數寫迴圈外面36   CGFloat topMargin = 30;37   38   //  匯入素材,plist檔案39   //  for迴圈建立app40   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設 frame58     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*)dic10 {11   return [[self alloc]initWithDic:dic];12 }13 //3.構造對象方法14 - (instancetype)initWithDic:(NSDictionary*)dic15 {16   if (self = [super init])17   {18     _name = dic[@"name"];19     _icon = dic[@"icon"];20   }21   return self;22 }23 //4.返回對象數組的類方法24 + (NSArray*)arrWithModels25 {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 @end15 16 @implementation CZAppView17 18 - (void)setApp:(CZApp *)app19 {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)appView32 {33   return [[[NSBundle mainBundle] loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];34 }35 36 37 @end

 

九宮格代碼--用了xib,plist載入和mvc思想

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.