九宫格布局(demo16.03.14)

//
//  ViewController.m
//  AppManager
//
//  Created by xin on 15-3-16.
//  Copyright (c) 2015年 Jackey. All rights reserved.
//

#import "ViewController.h"


//1 get the data
//2 draw the view
@interface ViewController ()

//
@property (nonatomic,strong) NSArray *appList;
@end

@implementation ViewController

-(NSArray *)appList{
    if(!_appList){
        NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
        _appList = [[NSArray alloc]initWithContentsOfFile:path];
    }
    return _appList;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    int totalCol = 3;
    CGFloat viewW = 80;
    CGFloat viewH = 90;
    
    CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);
    CGFloat marginY =10;
    CGFloat startY = 20;
    //int count = self.appList.count;
    for(int i=0;i<self.appList.count;i++){
        int row =i/totalCol;
        int col = i%totalCol;
        
        CGFloat x = marginX +(marginX+viewW)*col;
        CGFloat y =startY+ marginY +(marginY+viewH)*row;
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, y, viewW, viewH)];
        view.backgroundColor = [UIColor redColor];
        [self.view addSubview:view];
    }
    
}

@end

  

原文地址:https://www.cnblogs.com/lihaozhou/p/4342792.html