应用管理示例
本例中暂时没有使用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;
}