ios14--购物车优化2
2024-09-06 18:29:42
//
// ViewController.m
// 03-综合练习
//
// Created by xiaomage on 15/12/28.
// Copyright © 2015年 小码哥. All rights reserved.
// #import "ViewController.h" @interface ViewController () // 购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarView;
// 添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
// 删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton; /** 数据数组 */
@property (nonatomic, strong) NSArray *dataArr;
@end @implementation ViewController
/**
* 懒加载
1.作用:
1>用到的时候再加载
2>全局只会被加载一次
3>全局都可以使用 过程:
1.重写成员变量的get方法
2.在get方法中判断:
1>如果为空,加载数据
2>如果不为空,就直接返回数据
*/
- (NSArray *)dataArr{
if (_dataArr == nil) {
// 加载数据
self.dataArr = @[
@{@"name":@"单肩包", @"icon":@"danjianbao"},
@{@"name":@"钱包", @"icon":@"qianbao"},
@{@"name":@"链条包", @"icon":@"liantiaobao"},
@{@"name":@"手提包", @"icon":@"shoutibao"},
@{@"name":@"双肩包", @"icon":@"shuangjianbao"},
@{@"name":@"斜挎包", @"icon":@"xiekuabao"}
];
}
return _dataArr;
} // 初始化数据
- (void)viewDidLoad {
[super viewDidLoad]; // NSArray<NSDictionary *> *dataArr = @[
// @{@"name":@"单肩包", @"icon":@"danjianbao"},
// @{@"name":@"钱包", @"icon":@"qianbao"},
// @{@"name":@"链条包", @"icon":@"liantiaobao"},
// @{@"name":@"手提包", @"icon":@"shoutibao"},
// @{@"name":@"双肩包", @"icon":@"shuangjianbao"},
// @{@"name":@"斜挎包", @"icon":@"xiekuabao"}
// ];
// self.dataArr = dataArr;
} /**
* 添加到购物车
*
* @param button 按钮
*/
- (IBAction)add:(UIButton *)button {
/***********************1.定义一些常量*****************************/
// 1.总列数
NSInteger allCols = ;
// 2.商品的宽度 和 高度
CGFloat width = ;
CGFloat height = ;
// 3.求出水平间距 和 垂直间距
CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width) / (allCols -);
CGFloat vMargin = (self.shopCarView.frame.size.height - * height) / ;
// 4. 设置索引
NSInteger index = self.shopCarView.subviews.count;
// 5.求出x值
CGFloat x = (hMargin + width) * (index % allCols);
CGFloat y = (vMargin + height) * (index / allCols); /***********************2.创建一个商品*****************************/
// 1.创建商品的view
UIView *shopView = [[UIView alloc] init]; // 2.设置frame
shopView.frame = CGRectMake(x, y, width, height); // 3.设置背景颜色
shopView.backgroundColor = [UIColor greenColor]; // 4.添加到购物车
[self.shopCarView addSubview:shopView]; // 5.创建商品的UIImageView对象
UIImageView *iconView = [[UIImageView alloc] init];
iconView.frame = CGRectMake(, , width, width);
iconView.backgroundColor = [UIColor blueColor];
[shopView addSubview:iconView]; // 6.创建商品标题对象
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.frame = CGRectMake(, width, width, height - width);
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
[shopView addSubview:titleLabel]; /***********************3.设置数据*****************************/
// 方式四 (数组 + 字典)
/*
NSArray<NSDictionary *> *dataArr = @[
@{@"name":@"单肩包", @"icon":@"danjianbao"},
@{@"name":@"钱包", @"icon":@"qianbao"},
@{@"name":@"链条包", @"icon":@"liantiaobao"},
@{@"name":@"手提包", @"icon":@"shoutibao"},
@{@"name":@"双肩包", @"icon":@"shuangjianbao"},
@{@"name":@"斜挎包", @"icon":@"xiekuabao"}
];
*/
// 懒加载
/*
if (self.dataArr == nil) {
self.dataArr = @[
@{@"name":@"单肩包", @"icon":@"danjianbao"},
@{@"name":@"钱包", @"icon":@"qianbao"},
@{@"name":@"链条包", @"icon":@"liantiaobao"},
@{@"name":@"手提包", @"icon":@"shoutibao"},
@{@"name":@"双肩包", @"icon":@"shuangjianbao"},
@{@"name":@"斜挎包", @"icon":@"xiekuabao"}
];
}
*/
// 设置数据
NSDictionary *dict = self.dataArr[index];//调用get方法
iconView.image = [UIImage imageNamed:dict[@"icon"]];
titleLabel.text = dict[@"name"];
/***********************4.设置按钮的状态*****************************/ button.enabled = (index != ); // 5.设置删除按钮的状态
self.removeButton.enabled = YES; } /**
* 从购物车中删除
*
* @param button 按钮
*/
- (IBAction)remove:(UIButton *)button {
// 1. 删除最后一个商品
UIView *lastShopView = [self.shopCarView.subviews lastObject];
[lastShopView removeFromSuperview]; // 3. 设置添加按钮的状态
self.addButton.enabled = YES; // 4. 设置删除按钮的状态
/*
if (self.shopCarView.subviews.count == 0) {
self.removeButton.enabled = NO;
}
*/
self.removeButton.enabled = (self.shopCarView.subviews.count != ); }
@end
最新文章
- JSon解析
- MySql.Data.MySqlClient.MySqlException: Parameter ‘@maxid’ must be defined
- [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余
- [OSG]OSG例子程序简介
- win7安装nodejs
- poj 3744 Scout YYF I(概率dp,矩阵优化)
- ABAP 承运路单
- R语言与数据分析
- Node.js 相关资料网站汇总
- E3-1230和E3-1230 V2有多神?
- hdu1150-Machine Schedule(最小点覆盖)
- pathmunge /etc/profile
- 用CSS3实现带小三角形的div框(不用图片)
- 使用FileReader实现前端图片预览
- Java学习笔记——浅谈数据结构与Java集合框架(第二篇、Queue、Set)
- struts 中的创建Action的三种方法
- java io系列02之 ByteArrayInputStream的简介,源码分析和示例(包括InputStream)
- golang逃逸分析和竞争检测
- mysql数据库的备份和还原
- HDU 5069 Harry And Biological Teacher(AC自动机+线段树)
热门文章
- Redis系列(九)--几道面试题
- ThinkPHP---框架介绍
- [转]Js获取当前日期时间及其它操作
- 出现For input string: ";"; 错误
- IP地址、MAC地址、ARP地址解析协议
- P1060 开心的金明(洛谷,动态规划递推,01背包轻微变形题)
- [USACO] 奶牛混合起来 Mixed Up Cows
- python 使用time / datetime进行时间、时间戳、日期转换
- 每日命令:(10)cat
- python实现给定一个数和数组,求数组中两数之和为给定的数