实现九宫格的算法思路:
具体示例:
1//2//ViewController.m3//九宫格4//5//Created by 李前途 on 15/3/26.6//Copyright (c) 2015年 李前途. All rights reserved.7//89#import"ViewController.h"1011@interfaceViewController ()1213@PRoperty (nonatomic,strong) NSArray *data;14@end1516@implementationViewController1718- (NSArray *)data{19if(_data ==nil) {20NSString *fileName= [[NSBundle mainBundle] pathForResource:@"app.plist"ofType:nil];21_data =[NSArray arrayWithContentsOfFile:fileName];22}2324return_data;25}2627- (void)viewDidLoad {28[super viewDidLoad];2930intcolNumber =3;3132CGFloat appW =85;33CGFloat appH =90;343536CGFloat marginX = (self.view.frame.size.width - appW * colNumber) / (colNumber +1);37CGFloat marginY =15;3839for(intindex =0; index < self.data.count; index++) {40introw = index /colNumber;41intcol = index %colNumber;42CGFloat appX = col * (appW + marginX) +marginX;43CGFloat appY = row * (appH + marginY) +30;44UIView *appView =[[UIView alloc] init];45//appView.backgroundColor = [UIColor greenColor];46appView.frame =CGRectMake(appX, appY, appW, appH);47[self.view addSubview:appView];4849NSDictionary *dataDic =self.data[index];50UIImageView *iconImg =[[UIImageView alloc] init];51CGFloat iconW =45;52CGFloat iconH =45;53CGFloat iconX= (appW - iconW) /2;54CGFloat iconY =0;55iconImg.frame =CGRectMake(iconX, iconY, iconW, iconH);56iconImg.image = [UIImage imageNamed:dataDic[@"icon"]];57[appView addSubview:iconImg];585960UILabel *label =[[UILabel alloc] init];61CGFloat labelW =appW;62CGFloat labelH =25;63CGFloat labelX =0;64CGFloat labelY =iconH;65label.frame =CGRectMake(labelX, labelY, labelW, labelH);66label.text = dataDic[@"name"];67label.font = [UIFont systemFontOfSize:13];68label.textAlignment =NSTextAlignmentCenter;69[appView addSubview:label];7071UIButton *btn =[[UIButton alloc] init];72CGFloat btnX =12;73CGFloat btnY = labelY +labelH;74CGFloat btnW = appW -2*btnX;75CGFloat btnH =20;76btn.frame =CGRectMake(btnX, btnY, btnW, btnH);77[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];78[btn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];79[btn setTitle:@"下载"forState:UIControlStateNormal];80btn.titleLabel.font = [UIFont systemFontOfSize:11];81[appView addSubview:btn];8283}84858687}8889- (void)didReceiveMemoryWarning {90[super didReceiveMemoryWarning];91//Dispose of any resources that can be recreated.92}9394@end
后期可将字典转模型,以及封装代码。