A. In the code prior to separating view from Viewcontroller, the data load logic for view is placed in the total viewcontroller, adding coupling, and the details of the controller Viewcontroller hidden data should be loaded into the view. Encapsulates the view's creation logic to encapsulate the data load logic of the view into the custom UIView
B. Ideas to use the Xib package to customize the view: 1. Create a custom view that inherits UIView, where the name is "Appview", which encapsulates the group of controls for each appview of the standalone control group 2. Create a new Xib file to describe the control structure. is the control group 3. Use Appview as the type unit for each individual control group in the Controller 4. Connect the control to the View "Appview" 5.View "Appview" provides a model property 6. Overrides the setter of the model property, Parsing model data 7. Setting the model data into a control 8. Customize the construction method of the View "Appview" to mask the details of reading the Xib file
In fact, this is a simple MVC pattern model:app.h, app.m view:appview.h, appview.m controller:viewcontroller.h, VIEWCONTROLLER.M The controller connects the view and model, gets the data, loads it into the model, and passes it to the view for parsing and displaying
C. Implementation 1. New UIView class "Appview", inherited from UIView new file ==> iOS ==> Source ==> Cocoa Touch Class Create a claim file "AppView.h" and "Appview." M "A. B. 2. Set Xib Class (default is UIView) for new" Appview "
3. Data load logic (1) to write view in new UIView Create model member in "AppView.h"
// model data transferred between controller and view @property (nonatomic, strong) App *appdata;
(2) Connecting controls and "Appview", creating private control members
(3) Parsing load data in "APPVIEW.M"
-(void) Setappdata: (APP *= appData; // 1. Set picture self.iconView.image = [UIImage ImageNamed:appData.icon]; // 2. Set the name Self.nameLabel.text = appdata.name;}
(4) Custom construction method AppView.h:
//Customizing the construction method for loading model data into view-(Instancetype) Initwithapp: (APP *) AppData;//class methods for custom constructs+ (Instancetype) Appviewwithapp: (APP *) AppData;//returns a class construction method with no model data+(instancetype) Appview; APPVIEW.M://Customizing the construction method for loading model data into view-(Instancetype) Initwithapp: (APP *) AppData {//1. Getting controls from nibuinib *nib = [uinib nibwithnibname:@"app"Bundle:[nsbundle Mainbundle]]; Nsarray*viewarray =[nib Instantiatewithowner:nil options:nil]; Appview*appview =[Viewarray Lastobject];//2. Load ModelAppview.appdata =AppData;returnAppview;}//class methods for custom constructs+ (Instancetype) Appviewwithapp: (APP *) AppData {return[Self alloc] initwithapp:appdata];}//returns a class construction method with no model data+(instancetype) Appview {return[self appviewwithapp:nil];}
(5) Create "Appview" in the controller and load the data
// 1. Create a View Appview *appview = [Appview appviewwithapp:appdata]; // 2. Define the location and size of each app CGFloat AppX = marginx + column * (Marginx += marginy + row * (Marginy + app_height); = CGRectMake (AppX, Appy, App_width, app_height); // 3. Add this app info to total view [Self.view Addsubview:appview];
Main code VIEWCONTROLLER.M
#import "ViewController.h"#import "App.h"#import "AppView.h"#defineIcon_key @ "icon"#defineName_key @ "Name"#defineApp_width 85#defineApp_height 90#defineMargin_head 20#defineIcon_width 50#defineIcon_height 50#defineName_width App_width#defineName_height 20#defineDownload_width (app_width-20)#defineDownload_height 20@interfaceViewcontroller ()/** Store application information*/@property (nonatomic, strong) Nsarray*apps;//App List@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];//additional setup after loading the view, typically from a nib.[self Loadapps];}- (void) didreceivememorywarning {[Super didreceivememorywarning];//Dispose of any resources the can be recreated.}#pragmaMark gets the list of apps-(Nsarray *) Apps {if(Nil = =_apps) {//1. Get the full path of the plistNSString *path = [[NSBundle mainbundle] Pathforresource:@"app.plist"Oftype:nil];//2. Loading DataNsarray *dictarray =[Nsarray Arraywithcontentsoffile:path];//3. Convert all dictionaries inside the dictarray into a model and put them in a new arrayNsmutablearray *apparray =[Nsmutablearray array]; for(Nsdictionary *dictinchDictarray) {//3.1 Creating a Model objectAPP *app =[App appwithdictionary:dict];//3.2 Add to app array[Apparray Addobject:app]; }_apps=Apparray; }return_apps;}#pragmaMark loads the full list of apps-(void) Loadapps {intAppcolumncount =[self appcolumncount];intApprowcount =[self approwcount]; CGFloat Marginx= (Self.view.frame.size.width-app_width * appcolumncount)/(Appcolumncount +1); CGFloat Marginy= (Self.view.frame.size.height-app_height * approwcount)/(Approwcount +1) +Margin_head;intColumn =0;introw =0; for(intindex=0; index<self.apps.count; index++) {App*appdata =Self.apps[index];//1. Create a ViewAppview *appview =[Appview Appviewwithapp:appdata];//2. Define the location and size of each appCGFloat AppX = marginx + column * (Marginx +app_width); CGFloat Appy= Marginy + Row * (Marginy +app_height); Appview.frame=CGRectMake (AppX, Appy, App_width, app_height);//3. Add this app info to total view[Self.view Addsubview:appview]; Column++;if(Column = =Appcolumncount) {Column=0; Row++; } }}#pragmaMark calculates the number of columns-(int) Appcolumncount {intCount =0; Count= Self.view.frame.size.width/App_width;if((int) Self.view.frame.size.width% (int) App_width = =0) {Count--; }returncount;}#pragmaMark calculates the number of rows-(int) Approwcount {intCount =0; Count= (Self.view.frame.size.height-margin_head)/App_height;if((int) (Self.view.frame.size.height-margin_head)% (int) App_height = =0) {Count--; }returncount;}@end
APPVIEW.M:
#import "AppView.h"#import "App.h"//Encapsulating Private Properties@interfaceAppview ()//encapsulates a control in a view, allowing only its own access@property (Weak, nonatomic) Iboutlet Uiimageview *IconView, @property (weak, nonatomic) Iboutlet UILabel*Namelabel;@end@implementationAppview- (void) Setappdata: (APP *) AppData {//1. Assigning Medel members_appdata =AppData;//2. Setting up a pictureSelf.iconView.image =[UIImage ImageNamed:appData.icon];//3. Set the nameSelf.nameLabel.text =Appdata.name;}//Customizing the construction method for loading model data into view-(Instancetype) Initwithapp: (APP *) AppData {//1. Getting controls from nibuinib *nib = [uinib nibwithnibname:@"app"Bundle:[nsbundle Mainbundle]]; Nsarray*viewarray =[nib Instantiatewithowner:nil options:nil]; Appview*appview =[Viewarray Lastobject];//2. Load ModelAppview.appdata =AppData;returnAppview;}//class methods for custom constructs+ (Instancetype) Appviewwithapp: (APP *) AppData {return[Self alloc] initwithapp:appdata];}//returns a class construction method with no model data+(instancetype) Appview {return[self appviewwithapp:nil];}@end
app.m
#import "App.h"#defineIcon_key @ "icon"#defineName_key @ "Name"@implementationApp-(Instancetype) Initwithdictionary: (Nsdictionary *) Dictionary {if(self =[Super Init]) {Self.name=Dictionary[name_key];self.icon=Dictionary[icon_key]; }returnSelf ;}+ (Instancetype) appwithdictionary: (Nsdictionary *) Dictionary {//use self to represent the class name instead of the real class name to prevent child class invocation errorsreturn[Self alloc] initwithdictionary:dictionary];}@end
[iOS base Control-4.4] Further encapsulates the "app list", the beginning of the MVC pattern