////XMGVIEWCONTROLLER.M, CONTROLLER class#import "XMGViewController.h"#import "XMGShop.h"@interfaceXmgviewcontroller ()//Shopping Cart@property (Weak, nonatomic) Iboutlet UIView *Shopcarview;//Add button@property (Weak, nonatomic) Iboutlet UIButton *AddButton;//Delete button@property (Weak, nonatomic) Iboutlet UIButton *RemoveButton;/** Data Array*/@property (nonatomic, strong) Nsarray*Dataarr;@end@implementationXmgviewcontroller/** Lazy Loading*/-(Nsarray *) dataarr{if(_dataarr = =Nil) { //Loading Data//1. Get the full pathNSString *datapath = [[NSBundle mainbundle] Pathforresource:@"shopdata.plist"Oftype:nil];//supporting files below the Shopdata.plist file. Self.dataarr = [Nsarray Arraywithcontentsoffile:datapath];//DataPath = @ "/users/mctc/library/developer/coresimulator/devices/4e7e6ab7-bb75-4c2c-9d87-21a0369a3dd6/data/ containers/bundle/application/a855282b-7d1a-450a-b3c3-8ace93443577/03-comprehensive exercises. App/shopdata.plist " 0x00007fafe150ef70NSLog (@"%@", Self.dataarr);//The Get method was called. /*({icon = Danjianbao; Name = "\u5355\u5305"; }, {icon = Qianbao; Name = "\u94b1\u5305"; })*/ //dictionary to model object//creating a temporary arrayNsmutablearray *temparray =[Nsmutablearray array]; for(Nsdictionary *dictinch_dataarr) { //Create Shop Objects /*//Xmgshop *shop = [[Xmgshop alloc] initwithicon:dict[@ "icon"] name:dict[@ "name"]];//xmgshop *s Hop = [xmgshop shopwithicon:dict[@ "icon"] name:dict[@ "name"]];//shop.name = dict[@ "name"];//shop.i con = dict[@ "icon"]; */Xmgshop*shop =[Xmgshop shopwithdict:dict]; //load the model into an array[Temparray Addobject:shop]; } Self.dataarr=Temparray; } return_dataarr;}//Initializing Data- (void) viewdidload {[Super viewdidload]; /*class prefix//Foundation nsstring; Nsarray; Nsdictionary; Nsurl; UIKit UIView; Uiimageview; Uiswitch; */}/** * Add to Cart * * @param button*/-(Ibaction) Add: (UIButton *) button {/***********************1. Define some constants *****************************/ //1. Total number of columnsNsinteger Allcols =3; //2. Width and height of the productCGFloat width = the; CGFloat height= -; //3. Finding the horizontal and vertical spacingCGFloat Hmargin = (self.shopcarview.frame.size.width-allcols * width)/(Allcols-1); CGFloat Vmargin= (Self.shopCarView.frame.size.height-2* height)/1; //4. Setting the indexNsinteger index =Self.shopCarView.subviews.count; //5. Find the X valueCGFloat x = (hmargin + width) * (index%allcols); CGFloat y= (vmargin + height) * (Index/allcols); /***********************2. Create a product *****************************/ //1. Create a view of a productUIView *shopview =[[UIView alloc] init]; //2. Set the frameShopview.frame =CGRectMake (x, y, width, height); //3. Setting the background colorShopview.backgroundcolor =[Uicolor Greencolor]; //4. Add to Cart[Self.shopcarview Addsubview:shopview]; //5. Create a Uiimageview object for a productUiimageview *iconview =[[Uiimageview alloc] init]; Iconview.frame= CGRectMake (0,0, width, width); Iconview.backgroundcolor=[Uicolor Bluecolor]; [Shopview Addsubview:iconview]; //6. Create a product Title objectUILabel *titlelabel =[[UILabel alloc] init]; Titlelabel.frame= CGRectMake (0, width, width, height-width); Titlelabel.backgroundcolor=[Uicolor Yellowcolor]; Titlelabel.textalignment= Nstextalignmentcenter;//Center[Shopview Addsubview:titlelabel]; /***********************3. Setting up data *****************************/ //Setting up dataXmgshop *shop =Self.dataarr[index]; Iconview.image=[UIImage ImageNamed:shop.icon]; Titlelabel.text=Shop.name; /***********************4. Setting the state of the button *****************************/button.enabled= (Index! =5); //5. Set the status of the delete buttonself.removeButton.enabled =YES; }/** * Remove from cart * * @param button*/-(Ibaction) Remove: (UIButton *) button {//1. Delete the last itemUIView *lastshopview =[Self.shopCarView.subviews Lastobject]; [Lastshopview Removefromsuperview]; //3. Set the status of the Add buttonself.addButton.enabled =YES; //4. Set the status of the delete button /*if (Self.shopCarView.subviews.count = = 0) {self.removeButton.enabled = NO; } */self.removeButton.enabled= (Self.shopCarView.subviews.count! =0); }@end
////XMGShop.h#import<Foundation/Foundation.h>@interfaceXmgshop:nsobject/** Name of the picture*/@property (nonatomic, copy) NSString*icon;/** Name of the product*/@property (nonatomic, copy) NSString*name;//provides construction methods/*-(Instancetype) Initwithicon: (NSString *) icon Name: (NSString *) name;+ (instancetype) Shopwithicon: (NSString *) Icon Name: (NSString *) name; */-(Instancetype) Initwithdict: (Nsdictionary *) dict;+ (Instancetype) shopwithdict: (Nsdictionary *) dict;@end
////xmgshop.m#import "XMGShop.h"@implementationXmgshop/*-(Instancetype) Initwithicon: (NSString *) icon Name: (NSString *) name{if (self = [super init]) {Self.icon = Icon Self.name = name; } return self;} + (Instancetype) Shopwithicon: (NSString *) icon Name: (NSString *) name{return [[Self alloc] Initwithicon:icon name:name]; }*//*construction methods, general altogether object methods and class methods*/-(Instancetype) Initwithdict: (Nsdictionary *) dict{if(self =[Super Init]) {Self.icon= dict[@"icon"]; Self.name= dict[@"name"]; } returnSelf ;}+ (Instancetype) shopwithdict: (Nsdictionary *) dict{return[[Self alloc] initwithdict:dict];//Self alloc creates a class object. }@end
ios15--Comprehensive Small Example