Requirements 1. Display app information in n Gongge 2.APP information including icon, name, download button 3. Use as little code as possible, read app information from plist, calculate location size information for each app icon a. Thinking 1.UI layout: N Gongge 2. Event monitoring 3. Dynamic Add (by plist) 4. Overall package, combining each application information, using the view's tiered wrapper help layout B. Implementation (using pure code)
1 //2 //VIEWCONTROLLER.M3 //01-Application Management4 //5 //Created by Hellovoidworld on 14/11/24.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import "ViewController.h"Ten One #defineApp_width 85 A #defineApp_height 90 - #defineMargin_head 20 - #defineIcon_width 50 the #defineIcon_height 50 - #defineName_width App_width - #defineName_height 20 - #defineDownload_width (app_width-20) + #defineDownload_height 20 - + @interfaceViewcontroller () A at /** Store application information*/ -@property (nonatomic, strong) Nsarray *apps;//App List - - @end - - @implementationViewcontroller in -- (void) Viewdidload { to [Super Viewdidload]; + //additional setup after loading the view, typically from a nib. - the [self loadapps]; * } $ Panax Notoginseng- (void) didreceivememorywarning { - [Super didreceivememorywarning]; the //Dispose of any resources the can be recreated. + } A the #pragmaMark Gets the app list +-(Nsarray *) Apps { - if(Nil = =_apps) { $NSString *path = [[NSBundle mainbundle] Pathforresource:@"app.plist"Oftype:nil]; $_apps =[Nsarray Arraywithcontentsoffile:path]; - } - the return_apps; - }Wuyi the #pragmaMark loads all Apps list -- (void) Loadapps { Wu intAppcolumncount =[self appcolumncount]; - intApprowcount =[self approwcount]; About $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; A introw =0; + for(intindex=0; index<self.apps.count; index++) { theNsdictionary *appdata =Self.apps[index]; - $ //each app information theUIView *appview =[[UIView alloc] init]; the theCGFloat AppX = marginx + column * (Marginx +app_width); theCGFloat appy = marginy + row * (Marginy +app_height); - inAppview.frame =CGRectMake (AppX, Appy, App_width, app_height); the the //1. Set the app picture AboutUiimageview *iconview =[[Uiimageview alloc] init]; the theCGFloat Iconmarginx = (appview.frame.size.width-icon_width)/2; theCGFloat Iconmarginy =0; +Iconview.frame =CGRectMake (Iconmarginx, Iconmarginy, Icon_width, icon_height); - theIconview.image = [UIImage imagenamed:appdata[@"icon"]];Bayi [Appview Addsubview:iconview]; the the //2. Set the app name -UILabel *namelabel =[[UILabel alloc] init]; -Namelabel.text = appdata[@"name"]; theCGFloat Namemarginx = (appview.frame.size.width-name_width)/2; theCGFloat Namemarginy = Iconmarginy +Icon_height; theNamelabel.frame =CGRectMake (Namemarginx, Namemarginy, Name_width, name_height); the[Namelabel Setfont:[uifont Systemfontofsize: -]]; - [Namelabel Settextalignment:nstextalignmentcenter]; the [Appview Addsubview:namelabel]; the the //3. Set the download button94UIButton *downloadbutton =[UIButton Buttonwithtype:uibuttontypecustom]; theCGFloat Downloadmarginx = (appview.frame.size.width-download_width)/2; theCGFloat Downloadmarginy = Namemarginy +Name_height; theDownloadbutton.frame =CGRectMake (Downloadmarginx, Downloadmarginy, Download_width, download_height);98 AboutUIImage *downloadnormalimage = [UIImage imagenamed:@"Buttongreen"]; - [Downloadbutton setbackgroundimage:downloadnormalimage forstate:uicontrolstatenormal];101 102UIImage *downloadhighlightedimage = [UIImage imagenamed:@"buttongreen_highlighted"];103 [Downloadbutton setbackgroundimage:downloadhighlightedimage forstate:uicontrolstatehighlighted];104 the[Downloadbutton Settitle:@"Download"Forstate:uicontrolstatenormal];106 107 //do not remove the Titlelabel property directly, as this will not set the full state108[Downloadbutton.titlelabel Setfont:[uifont Systemfontofsize: -]];109 the [Appview Addsubview:downloadbutton];111 the 113 [Self.view Addsubview:appview]; the thecolumn++; the if(Column = =Appcolumncount) {117Column =0;118row++;119 } - }121 }122 123 #pragmaMark calculates the number of columns124- (int) Appcolumncount { the intCount =0;126Count = self.view.frame.size.width/App_width;127 - if((int) Self.view.frame.size.width% (int) App_width = =0) {129count--; the }131 the returncount;133 }134 135 #pragmaMark calculates the number of rows136- (int) Approwcount {137 intCount =0;138Count = (self.view.frame.size.height-margin_head)/App_height;139 $ if((int) (Self.view.frame.size.height-margin_head)% (int) App_height = =0) {141count--;142 }143 144 returncount;145 }146 @end
#1. Files that have been placed in Images.xcassets can only be accessed through [UIImage imagenamed: (NSString *) ImageName]?
[iOS base Control-4.1] App List