Xcode-程序开发设计-02九宫格

行号是除 决定Y值

列号是余 决定X值

 

//
//  ViewController.m
//  06-应用管理
//
//  Created by daier on 15/12/31.
//  Copyright © 2015年 daier. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *apps;

@end

@implementation ViewController

- (NSArray *)apps
{
    if (_apps == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        _apps = [NSArray arrayWithContentsOfFile:path];
    }
    NSLog(@"%ld",_apps.count);
    
    return _apps;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat appW = 85;
    CGFloat appH = 90;
    int totalColoumns = 3;
    
    CGFloat marginX = (self.view.frame.size.width - totalColoumns*appW)/(totalColoumns+1);
    CGFloat marginY = 20;
    
    for (int index = 0; index < self.apps.count; index++) {
        UIView *appView = [[UIView alloc] init];
        appView.backgroundColor = [UIColor redColor];
        
        int row = index/totalColoumns;
        int col = index%totalColoumns;
        
        CGFloat appX = marginX + col*(appW + marginX);
        CGFloat appY = 30 + row*(appH + marginY);
        
        appView.frame = CGRectMake(appX, appY, appW, appH);
        [self.view addSubview:appView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

posted @ 2016-01-08 14:00  呆而肥  阅读(237)  评论(0编辑  收藏  举报