Further optimization of the code for iOS development fundamentals-nine grid coordinates (3).
Create a new subclass of UIView , named Wjqappview , to associate the UIView object in appxib.xib with the new view class.
The Wjqappview class declares 3 Iboutlet properties, with Uiimageview , UILabel, contained in the View object in appxib.xib establish a connection with UIButton . The wjqappview header file code looks like this:
1 //wjqappview.h 2 @interface Wjqappview:uiview 3 @property (weak, nonatomic) iboutlet Uiimageview *appimage; 4 @property (weak, nonatomic) iboutlet UILabel *Applabel; 5 @property (weak, nonatomic) iboutlet UIButton *Appbutton; 6 @end
Finally, modify the viewdidload method in viewcontroller.m :
1 //VIEWCONTROLLER.M2- (void) Viewdidload {3 [Super Viewdidload];4 5 intTotalcolumn =3;//3 Columns6CGFloat margin = (self.view.frame.size.width-totalcolumn*appviewwidth)/(Totalcolumn +1);7 intCount =Self.apps.count;8NSLog (@"%d", count);9 Ten for(inti =0; I < count; i++) { One introw = I/totalcolumn;//line number, starting from 0 A intcolumn = I%totalcolumn;//column number, starting from 0 -CGFloat appviewx = margin + (margin + appviewwidth) * column;//x-coordinate of the child view -CGFloat appviewy = margin + (margin + appviewheight) * ROW;//the y-coordinate of the child view theWjqappinfo *appinfo =Self.apps[i]; - - //Creating UIView Controls -Nsarray *apparray = [[NSBundle mainbundle] loadnibnamed:@"Appxib"Owner:nil Options:nil]; +Wjqappview *appview =[Apparray Firstobject]; -Appview.frame =CGRectMake (appviewx, Appviewy, Appviewwidth, appviewheight); +AppView.appImage.image = Appinfo.image;//set up a picture AAppView.appLabel.text = Appinfo.desc;//Set name atAppView.appButton.tag = i;//set the sequence number of the button - [Appview.appbutton addtarget:self Action: @selector (buttonclicked:) forcontrolevents:uicontroleventtouchupinsi De]; - - [Self.view Addsubview:appview]; - } -}
Reference blog: Simple use of iOS development UI Chapter-xib
Instance code: HTTP://PAN.BAIDU.COM/S/1O7L6IXC
iOS Development Basics-Nine grid coordinates (4)