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 = ...
随机推荐
- HDU_1874_畅通工程续_最短路问题
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- swift 扩展 要素总结
类: 协议: 泛型及元素类型:扩展约束:
- CAD使用GetxDataString读数据(网页版)
主要用到函数说明: MxDrawEntity::GetxDataString2 读取一个字符扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, retval ...
- Eureka组件、Eureka自我保护模式
Eureka包含两个组件:Eureka Server和Eureka Client Eureka Server提供服务发现的能力,各个微服务启动时,会向Eureka Server注册自己的信息(例如 ...
- [luogu3067 USACO12OPEN] 平衡的奶牛群
传送门 Solution 折半搜索模板题 考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可 Code //By Menteur_Hxy #include <cmath> #i ...
- MySql报Packet for query is too large错误
mysql中执行sql的时候报以下错误:Packet for query is too large (1354 > 1024) 原因是mysql一次接收的报文太长,需要调整服务器参数max_al ...
- Hashing - Hard Version
Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...
- WeChat-小程序-tabbar
WeChat-小程序-tabbar https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#%E5%85%A8%E ...
- 戏说云计算之PaaS,IaaS,SaaS
最近我们聊到"CRM系统PAAS化",有些可能就不了解,到底什么是PAAS.云计算还有IaaS,SaaS概念,这三者之间有什么区别?今天智云通CRM系统小编用通俗易懂的例子跟大家分 ...
- java增强for循环中获取index
java增强for循环中获取index http://rensanning.iteye.com/blog/2003205