Story Board Controller:
////XMGVIEWCONTROLLER.M//03-Comprehensive exercises////Created by Xiaomage on 15/12/28.//2015 little brother. All rights reserved.//#import "XMGViewController.h"#import "XMGShop.h"#import "XMGShopView.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]; Self.dataarr=[Nsarray Arraywithcontentsoffile:datapath]; //Dictionary Turn Model//creating a temporary arrayNsmutablearray *temparray =[Nsmutablearray array]; for(Nsdictionary *dictinch_dataarr) { //Create Shop ObjectsXmgshop *shop =[Xmgshop shopwithdict:dict]; //load the model into an array[Temparray Addobject:shop]; } Self.dataarr=Temparray; } return_dataarr;}//Initializing Data- (void) viewdidload {[Super viewdidload];}/** * 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 *****************************/Xmgshopview*shopview =[Xmgshopview Shopview]; Shopview.frame=CGRectMake (x, y, width, height); //Setting up dataShopview.shop = Self.dataarr[index];//Set Method[Self.shopcarview Addsubview:shopview];/***********************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 buttonself.removeButton.enabled = (Self.shopCarView.subviews.count! =0); }@end
Xib Interface:
Xib interface corresponding to the class:
// // XMGShopView.h#import <UIKit/UIKit.h>@class xmgshop; @interface Xmgshopview:uiview /* * * Shop; // Rapid Construction Method + (instancetype) Shopview; @end
////xmgshopview.m#import "XMGShopView.h"#import "XMGShop.h"@interfaceXmgshopview () @property (weak, nonatomic) Iboutlet Uiimageview*iconview;//It's connected to the Xib interface.@property (Weak, nonatomic) Iboutlet UILabel *titlelabel;//It's connected to the Xib interface.@end@implementationXmgshopview+(instancetype) shopview{//return [[[NSBundle Mainbundle] loadnibnamed:@ "Xmgshopview" Owner:nil Options:nil] firstobject]; //Xmgshopview is Xib's name. return[[[ NSBundle Mainbundle] Loadnibnamed:nsstringfromclass (self) owner:nil options:nil] firstobject];}//-(void) awakefromnib//Gazi Controls- (void) Setshop: (Xmgshop *) shop{_shop=Shop ; //set the interface picture and textSelf.iconView.image =[UIImage ImageNamed:shop.icon]; Self.titlelabel.text=Shop.name;}@end
The data bean class for the Xib interface:
////XMGShop.h//03-Comprehensive exercises////Created by Xiaomage on 15/12/29.//2015 little brother. All rights reserved.//#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//03-Comprehensive exercises////Created by Xiaomage on 15/12/29.//2015 little brother. All rights reserved.//#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]; } */-(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];}@end
Story Board:
ios21--xib Example