ios13--购物车优化
//
// ViewController.m
// 03-综合练习
// #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, assign) NSInteger index; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
} /**
* 添加到购物车
*
* @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.设置数据*****************************/
// 数值数据
/*
// 方式一: (不可取:数据都是一样)
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
*/ // 方式二 (不可取:太冗余)
/*
if (index == 0) {
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
}else if (index == 1){
iconView.image = [UIImage imageNamed:@"qianbao"];
titleLabel.text = @"钱包";
}else if (index == 2){
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
}else if (index == 3){
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
}else if (index == 4){
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
}else if (index == 5){
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
}
*/ // 方式三 (数组: (两个数组之间没有任何联系,容易出错))
/*
NSArray<NSString *> *imageNames = @[@"danjianbao", @"qianbao", @"liantiaobao", @"shoutibao", @"shuangjianbao", @"xiekuabao"];
NSArray<NSString *> *titleNames = @[@"单肩包", @"钱包", @"链条包", @"手提包", @"双肩包", @"斜挎包"];
// 设置数据
iconView.image = [UIImage imageNamed:imageNames[index]];
titleLabel.text = titleNames[index];
*/ // 方式四 (数组 + 字典)
NSArray<NSDictionary *> *dataArr = @[
@{@"name":@"单肩包", @"icon":@"danjianbao"},
@{@"name":@"钱包", @"icon":@"qianbao"},
@{@"name":@"链条包", @"icon":@"liantiaobao"},
@{@"name":@"手提包", @"icon":@"shoutibao"},
@{@"name":@"双肩包", @"icon":@"shuangjianbao"},
@{@"name":@"斜挎包", @"icon":@"xiekuabao"}
];
// 设置数据
NSDictionary *dict = dataArr[index];
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
ios13--购物车优化的更多相关文章
- python之作业--------购物车优化
Read Me:继上次简单购物车的实现,有再一次的升级优化了下,现实现以下几个功能: 1.有客户操作和商家操作,实现,客户可以买东西,当金额不足提醒,最后按q退出,打印购物车列表 2.商家可以添加操作 ...
- python购物车优化
一.需求分析 拥有用户接口和商家接口 用户能够进行消费记录查询,充值,购物等功能,消费记录存储于数据库 商家可以进行商品的增删改等操作 二.程序流程图 程序大致流程图如下: 三.代码实现 本程序分成两 ...
- Python-文件操作-之优化购物车
#此次购物车优化,主要使用了文件操作的相关方法,有买家入口,和商家入口 一.买家入口 1.买家第一次启动程序输入金额,金额会记录到文件里,再登录就读取文件里保存的金额,买家可以购买商品,按 ‘q’ 退 ...
- Python之作业购物车
作业之购物车优化 购物车优化要求如下: 用户入口: 启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就 ...
- Python 实例2—购物车
老男孩教学学习笔记 """启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒可随机退出,退出 ...
- 老男孩python作业3-购物车程序优化
购物车优化要求:用户入口: 1.商品信息存在文件里 2.已购商品,余额记录.第一次启动程序时需要记录工资,第二次启动程序时谈出上次余额 3.允许用户根据商品编号购买商品 4.用户选择商品后,检测是否够 ...
- Python之购物车
Python之购物车 msg_list = [ ['iphone',8888], ['coffe',38], ['book',90], ['Tesla',100000], ['RR',10000000 ...
- Python基础-week02
本节内容摘要:http://www.cnblogs.com/Jame-mei 0.xmind8破解及安装(想要破解版及资料请下方留言,这里不做具体说明) 1.模块初识 2.pyc是什么? 3.pyth ...
- python 自学之路-Day Two
Day1补充部分 模块初识 模块就是由其他人写好的功能,在程序需要的时候进行导入,直接使用,也叫库. 库有标准库和第三方库,所谓标准库,就是不需要安装就可以直接使用的,自带的:第三方库,就是需要进行下 ...
- Python运维开发基础05-语法基础【转】
上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...
随机推荐
- Windows提高_2.3第三部分:内核区同步
第三部分:内核区同步 等待函数(WaitForObject) 等待函数的形式 单个:WaitForSingleObject 多个:WaitForMultipleObjects 一个可以被等待的对象通常 ...
- Jmeter之关联——常用提取器
Jmeter关联 所谓关联,从业务角度讲,即:某些操作步骤与其相邻步骤存在一定的依赖关系,导致某个步骤的输入数据来源于上一步的返回数据,这时就需要“关联”来建立步骤之间的联系. 简单来说,就是:将上一 ...
- blender--(凹凸贴图)................https://jingyan.baidu.com/article/9f63fb917c4becc8400f0ea8.html
在blender中直接绘制模型凹凸纹理细节 听语音 | 浏览:32 | 更新:2018-02-20 11:18 1 2 3 4 5 6 7 分步阅读 在blender中为了表现更多的模型细节,我们会常 ...
- java_IO_3
Reader和Writer针对字符文件 对图片类文件可能就显得无能为力了 会损坏文件 package ioStudy; import java.io.File; import java.io.Fi ...
- linux ssh 利用scp传输文件
使用方式如下: 1.上传本地文件到服务器 scp /path/filename username@servername:/path/ 例如scp /var/www/test.php root@192. ...
- 利用freemarker+SAX解析xml的方式对excel文件字段校验
利用freemarker对参数进行校验这篇文章主要用到的技术点: 自定义注解的使用反射机制SAX解析xmlFreemarker的运用我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验 ...
- S-HR之代码创建临时表并插入数据
... private String tempTab1 = null; //临时表EcirrWithPPTempTable public String getTempTable() { String ...
- zend studio 12.5.1 window 安装并设置笔记
下载百度云上的安装包-----------------------------------------------安装时,解决window 10的权限问题 安装完成后的设置设置字体window=> ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- [bzoj1572][Usaco2009 Open]工作安排Job_贪心_堆
工作安排 Job bzoj-1572 Usaco-2009 Open 题目大意:题目链接. 注释:略. 想法: 我们将任务按照截止时间排序,枚举任务的同时顺便记录出已经做了多少任务(当前时间). 对于 ...