#import "ViewController.h"#import "CZapp.h" //Referencing model classes@interfaceViewcontroller () @property (nonatomic, strong) Nsarray* APPS;//declaring an array to hold data@end@implementationViewcontroller//Lazy Loading-(Nsarray *) Apps {if(_apps==Nil) { //Get plist pathnsstring* path = [[NSBundle mainbundle] Pathforresource:@"apps_plist"Oftype:nil]; //get the data based on the arraynsarray* arraydict =[Nsarray Arraywithcontentsoffile:path]; //declares a mutable array to hold the dataNsmutablearray * Arraymodels =[Nsmutablearray array]; //Loop Dictionary assigns a value to a model for(Nsdictionary * dictincharraydict) { //call Czapp dictionary to modelCzapp *model =[Czapp appwith:dict]; //model assignment to a mutable array[Arraymodels Addobject:model]; } } return_apps;}- (void) viewdidload {[Super viewdidload]; /*calculation steps: 1. Determine the width and height of each app 2. Calculate Marginx,marginy,margintop 3. Calculates the row index for each app, and the column index 4. Based on the row index and column of the current app The index calculates the appx and Appy*/ //define a variable to be 3 columns intcloumns=3; //get the width of the viewCGFloat viewwidth =Self.view.frame.size.width; CGFloat APPW= the; CGFloat APPH= -; //the distance from the top of the first rowCGFloat margintop = -; CGFloat Marginx= (VIEWWIDTH-CLOUMNS*APPW)/(cloumns+1); CGFloat Marginy=marginx;//Suppose the spacing between each line is equal to Marginx//Looping Create UIView for(inti =0; i< A; i++) { //calculate the index of the column that contains each cell intColidx =i%Cloumns; //calculate the index of the row for each cell intRowidx = I/Cloumns; //Create UIViewUIView *appsview =[[UIView alloc]init]; //set the view background colorAppsview.backgroundcolor =[Uicolor Bluecolor]; CGFloat AppX=marginx + colidx * (APPW +Marginx); CGFloat Appy=margintop +rowidx * (APPH +marginy); Appsview.frame=CGRectMake (AppX, Appy, APPW, APPH); [Self.view Addsubview:appsview]; //Create UiimageviewUiimageview * ImageIcon =[[Uiimageview alloc]init]; Imageicon.backgroundcolor=[Uicolor Greencolor]; CGFloat iconw= $; CGFloat Iconh= $; CGFloat IconX= (APPSVIEW.FRAME.SIZE.WIDTH-ICONW) *0.5; CGFloat Icony=0; Imageicon.frame=CGRectMake (IconX, Icony, Iconw, Iconh); [Appsview Addsubview:imageicon]; //Create lableUILabel *lblname=[[UILabel alloc] init]; Lblname.backgroundcolor=[Uicolor Yellowcolor]; CGFloat LBLW=AppsView.frame.size.width; CGFloat LBLH= -; CGFloat LBLX=0; CGFloat lbly=Iconh; Lblname.frame=CGRectMake (LBLX, lbly, LBLW, LBLH); [Appsview Addsubview:lblname]; //Create buttonUIButton * Btndown =[[UIButton alloc]init]; Btndown.backgroundcolor=[Uicolor Redcolor]; CGFloat BTNW=iconw; CGFloat BTNH= -; CGFloat btnx=IconX; //cgfloat Btny =lbly+lblh;CGFloat Btny =Cgrectgetmaxy (lblname.frame); Btndown.frame=CGRectMake (btnx, Btny, BTNW, BTNH); [Appsview Addsubview:btndown]; }}@end
#import<Foundation/Foundation.h>@interfaceCzapp:nsobject@property (nonatomic,copy) NSString*Name: @property (nonatomic,copy) NSString*Icon // declaration properties
-(Instancetype) Initwithapp: (nsdictionary*) Dict;
+ (Instancetype) Appwith: (nsdictionary*) dict;@end////czapp.m//Uiviewcode#import "CZapp.h"@implementationThe role of Czapp//Instancetype is to enable methods of non-associative return types to return the type of the class -(Instancetype) Initwithapp: (nsdictionary*) dict{if(Self = =[Super Init]) {Self.name=dict[@"name"]; Self.icon=dict[@"icon"]; } returnSelf ;}+ (Instancetype) Appwith: (nsdictionary*) dict{return[Self alloc] initwithapp:dict];}@end
Nine Gongge calculations, creating application examples, dictionary to model