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
| #import "ViewController.h" #import "AppInfo.h" #import "AppView.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=[AppInfo appList]; } 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<self.appList.count; 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); //从XIB来加载自定义视图 AppView *appView=[[[NSBundle mainBundle]loadNibNamed:@"AppView" owner:nil options:nil] lastObject]; //设置视图的位置 appView.frame=CGRectMake(x, y, kAppViewW, kAppViewH); [self.view addSubview:appView]; //AppInfo *appInfo=self.appList[i]; appView.appInfo=self.appList[i]; } } @end
|