IOS2.1-应用管理示例(plist,模型,xib封装综合小示例)

应用管理示例                                                                                                     

 

本例中暂时没有使用UICollectionView 做九宫格

 

一:预期效果

 

 

二:代码展示

#import "ViewController.h"
#import "WYAppModel.h"
#import "WYAppView.h"
@interface ViewController ()<UIAlertViewDelegate>
/** 存放模型数据的数组*/
@property (nonatomic,strong)NSArray *appArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubView];
    
    //两种弹框的弹框的现实(UIAlertView和UIActionSheet)
    //UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"标题" message:@"内容" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"other", nil];
    //[alter show];
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"标题" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"重要按钮" otherButtonTitles:@"otherButton", nil];
    [sheet showInView:self.view];
    
}

//初始化控件

-(void)initSubView
{
    int totalColumns = 3;
    int appW = 85;
    int appH = 100;
    //循环创建UIView
    
    for (int i = 0; i<self.appArray.count; i++) {
        //1:取出xib(WYAppView已经封装好了)
         WYAppView *appView =[WYAppView appView];

        //2:设置UIView的长度
        int row = i/totalColumns;
        int col = i %totalColumns;
        int margainX  = (self.view.frame.size.width - totalColumns*appW)/(totalColumns +1);
        int margainY = 20;
        int appX = margainX + (appW +margainX)*col;
        int appY = margainY + (appH + 10)*row;
        appView.frame  = CGRectMake(appX, appY, appW, appH);
        
        //4:传递模型数据,使模型中的数据能够传到xib中显示出来
        appView.appModel = self.appArray[i];
        
        //3:加入到View里面
        [self.view addSubview:appView];
        
    }
    
}
//懒加载
-(NSArray *)appArray
{
    if (_appArray == nil) {
        
        //1:取出字典
        NSArray *appArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
        //2:字典转模型
        NSMutableArray *dictArray = [NSMutableArray array];
        for (NSDictionary *dict in appArray) {
            WYAppModel *appmodel = [WYAppModel appWithDict:dict];
            [dictArray addObject:appmodel];
        }
        _appArray = dictArray;
    }
    return _appArray;
}

@end

封装类的详细.h和.m文件见上篇文章

@implementation WYAppView

-(void)setApp:(appModel *)app
{
    _app = app;
    self.iconView.image = [UIImage imageNamed:app.icon];
    self.nameLable.text = app.name;
}

+(instancetype)appWithView
{
    NSBundle *bundal = [NSBundle mainBundle];
    NSArray *objs = [bundal loadNibNamed:@"WYAppView" owner:nil options:nil];
    WYAppView * appView = [objs lastObject];
    return appView;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值