解析iOS应用的UI开发中懒加载和xib的简单使用方法

2020-01-14 18:19:35王振洲

        
        //字典转模型
        NSMutableArray *arrayM=[NSMutableArray array ];
        for (NSDictionary *dict in temparray) {
            [arrayM addObject:[YYapp appWithDict:dict]];
        }
        _app=arrayM;
    }
    return _app;
}

//创建界面原型
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%d",self.app.count);
    
    //九宫格布局
    int totalloc=3;
    CGFloat appviewW=80;
    CGFloat appviewH=90;
    CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);
    
    int count=self.app.count;
    for (int i=0; i<count; i++) {
        
        int row=i/totalloc;
        int loc=i%totalloc;
        CGFloat appviewX=margin + (margin +appviewW)*loc;
        CGFloat appviewY=margin + (margin +appviewH)*row;
        YYapp *app=self.app[i];
        
        //拿出xib视图
       NSArray  *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];
        
        //注意这里的类型名!
        //UIView *appview=[apparray firstObject];
        YYappview  *appview=[apparray firstObject];
       
        //加载视图
        appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);
          [self.view addSubview:appview];
        
        appview.appimg.image=app.image;
        appview.applab.text=app.name;
        appview.appbtn.tag=i;