1.知识点:数组,字典,UIImageView,UILabel,UIButton
2.初始化数组:@property(nonatomic,strong) NSArray *apps;
- (NSArray *)apps
{
if (_apps == nil) {
// 1.获得plist的全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
// 2.加载数组
_apps = [NSArray arrayWithContentsOfFile:path];
}
return _apps;
}
3.加载图片,设置lable和button
- (void)viewDidLoad
{
[super viewDidLoad];
// self.apps;
// 添加应用
// 应用的尺寸
CGFloat appW = 85;
CGFloat appH = 90;
// 总列数
int totalColumns = 3;
// 间隙 = (控制器view的宽度 - 3*应用宽度) / 4
CGFloat margin = (self.view.frame.size.width - totalColumns*appW) / (totalColumns + 1);
CGFloat appX = margin;
CGFloat appY = 30;
加12个UIVIiew小框框
// 调用apps的getter方法
for (int index = 0; index < self.apps.count; index ++) {
// 行号
int line = index / totalColumns;
// 列号
int row = index % totalColumns;
UIView *appView = [[UIView alloc]init];
// appView.backgroundColor = [UIColor redColor];
appView.frame = CGRectMake(appX + (appW + margin)*row, appY + (appH + margin)*line, appW, appH);
[self.view addSubview:appView];
NSDictionary *appInfo = self.apps[index];
// 添加内部小组件
// 添加图片
UIImageView *imageView = [[UIImageView alloc]init];
CGFloat iconW = 60;
CGFloat iconH = 50;
CGFloat iconX = (appW - iconW)*0.5;
CGFloat iconY = 0;
imageView.frame = CGRectMake(iconX, iconY, iconW, iconH);
// imageView.backgroundColor = [UIColor blackColor];
// 加载图片
imageView.image = [UIImage imageNamed:appInfo[@"icon"]];
[appView addSubview:imageView];
// 添加名字
UILabel *nameLabel = [[UILabel alloc]init];
CGFloat nameW = 65;
CGFloat nameH = 20;
CGFloat nameX = (appW - nameW)*0.5;
CGFloat nameY = iconY + iconH;
nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
// nameLabel.backgroundColor = [UIColor yellowColor];
// 加载名字
nameLabel.text = appInfo[@"name"];
nameLabel.font = [UIFont systemFontOfSize:12]; // 字体大小
nameLabel.textAlignment = NSTextAlignmentCenter; // 字体居中
[appView addSubview:nameLabel];
// 添加下载按钮
UIButton *button =[[UIButton alloc]init];
CGFloat buttonW = 65;
CGFloat butttonH = 20;
CGFloat buttonX = (appW - nameW)*0.5;
CGFloat buttonY = iconY + iconH + nameH;
button.frame = CGRectMake(buttonX, buttonY, buttonW, butttonH);
button.backgroundColor = [UIColor grayColor];
// 设置普通状态下的图片
UIImage *normalImage = [UIImage imageNamed:@"buttongreen"];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
// 设置高亮状态下的图片
UIImage *highImage = [UIImage imageNamed:@"buttongreen_highlighted"];
[button setBackgroundImage:highImage forState:UIControlStateHighlighted];
[button setTitle:@"下载" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:13]; // 按钮改变字体 按钮内部有个label
[appView addSubview:button];
}
}
4.Button类里有UILabel,UIImageView属性,设置Button的字体的时候,通过label设置
button.titleLabel.font = [UIFont systemFontOfSize:13]; // 按钮改变字体 按钮内部有个label
5.开发中应尽可能加大代码的可扩展性增强,不仅仅是实现了现有的功能
通过把fram的各个属性拿出来单独赋值---方便修改每个视图的位置(当几个视图同时有一个父视图的情况更加明显)
根据索引取出数组元素即对应的字典 通过字典导出应用图标对应图片的名称 通过对用字典的key值(name)导出应用的名称
6.搭建九宫格的步骤
明确每一块用的是什么view
明确每个view之间的父子关系
先尝试逐个添加格子,最后考虑使用for循环
加载app数据,根据数据长度创建对应个数的格子
添加格子内部的子控件
给格子内部的子控件装配数据