1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #import "ViewController.h" #import "AppInfo.h" //九宫格常量 #define kAppViewW 80 //宽 #define kAppViewH 90 //高 #define kColCount 3 //列 #define kStartY 20 @interface ViewController () @property (nonatomic,strong) NSArray *appList; @end @implementation ViewController
- (NSArray *)appList { if (_appList==nil) { // _appList=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]]; NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]]; //创建一个临时数组 NSMutableArray *arrayM=[NSMutableArray array]; //遍历数组,一次类型转换 for (NSDictionary *dict in array) { //AppInfo *appInfo=[[AppInfo alloc]initWithDict:dict]; AppInfo *appInfo=[AppInfo appInfoWithDict:dict]; // appInfo.name=dict[@"name"]; // appInfo.icon=dict[@"icon"]; [arrayM addObject:appInfo]; } //将临时数组为属性赋值 _appList=arrayM; } return _appList; } - (void)viewDidLoad { [super viewDidLoad]; //九宫格界面 CGFloat marginX=(self.view.bounds.size.width-kColCount*kAppViewW)/(kColCount+1); CGFloat marginY=10; for (int i=0; i<10; i++) { //行 // 0,1,2 ->0 //3,4,5->1 int row=i/kColCount; //列 //0,3,6->0 //1,4,7->1 //2,5,8->2 int col=i%kColCount; CGFloat x=marginX+col*(marginX+kAppViewW); CGFloat y=kStartY+ marginY+row*(marginY+kAppViewH); UIView *appView=[[UIView alloc]initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)]; //appView.backgroundColor=[UIColor redColor]; [self.view addSubview:appView]; //NSDictionary *dict=self.appList[i]; AppInfo *appInfo=self.appList[i]; //九宫格背景色 //1->UIImageView UIImageView *icon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kAppViewW, 50)]; //icon.backgroundColor=[UIColor greenColor]; //设置图像 //icon.image=[UIImage imageNamed:dict[@"icon"]]; icon.image=[UIImage imageNamed:appInfo.icon]; //图像填充 icon.contentMode=UIViewContentModeScaleAspectFit; [appView addSubview:icon]; //2->UILabel->应用程序名称 UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(icon.frame), kAppViewW, 20)]; //label.backgroundColor=[UIColor blueColor]; //设置程序名称 //label.text=dict[@"name"]; label.text=appInfo.name; //字体 label.font=[UIFont systemFontOfSize:13.0]; label.textAlignment=NSTextAlignmentCenter; [appView addSubview:label]; //3->UIButton->下载按钮 UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame), kAppViewW, 20)]; //button.backgroundColor=[UIColor yellowColor]; //设置背景图片 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted]; [button setTitle:@"下载" forState:UIControlStateNormal]; //字体 button.titleLabel.font=[UIFont systemFontOfSize:12.0]; [appView addSubview:button]; } }
@end
|