//
// 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--购物车优化的更多相关文章

  1. python之作业--------购物车优化

    Read Me:继上次简单购物车的实现,有再一次的升级优化了下,现实现以下几个功能: 1.有客户操作和商家操作,实现,客户可以买东西,当金额不足提醒,最后按q退出,打印购物车列表 2.商家可以添加操作 ...

  2. python购物车优化

    一.需求分析 拥有用户接口和商家接口 用户能够进行消费记录查询,充值,购物等功能,消费记录存储于数据库 商家可以进行商品的增删改等操作 二.程序流程图 程序大致流程图如下: 三.代码实现 本程序分成两 ...

  3. Python-文件操作-之优化购物车

    #此次购物车优化,主要使用了文件操作的相关方法,有买家入口,和商家入口 一.买家入口 1.买家第一次启动程序输入金额,金额会记录到文件里,再登录就读取文件里保存的金额,买家可以购买商品,按 ‘q’ 退 ...

  4. Python之作业购物车

    作业之购物车优化 购物车优化要求如下: 用户入口: 启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就 ...

  5. Python 实例2—购物车

    老男孩教学学习笔记 """启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒可随机退出,退出 ...

  6. 老男孩python作业3-购物车程序优化

    购物车优化要求:用户入口: 1.商品信息存在文件里 2.已购商品,余额记录.第一次启动程序时需要记录工资,第二次启动程序时谈出上次余额 3.允许用户根据商品编号购买商品 4.用户选择商品后,检测是否够 ...

  7. Python之购物车

    Python之购物车 msg_list = [ ['iphone',8888], ['coffe',38], ['book',90], ['Tesla',100000], ['RR',10000000 ...

  8. Python基础-week02

    本节内容摘要:http://www.cnblogs.com/Jame-mei 0.xmind8破解及安装(想要破解版及资料请下方留言,这里不做具体说明) 1.模块初识 2.pyc是什么? 3.pyth ...

  9. python 自学之路-Day Two

    Day1补充部分 模块初识 模块就是由其他人写好的功能,在程序需要的时候进行导入,直接使用,也叫库. 库有标准库和第三方库,所谓标准库,就是不需要安装就可以直接使用的,自带的:第三方库,就是需要进行下 ...

  10. Python运维开发基础05-语法基础【转】

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...

随机推荐

  1. Sturts2几个常用内建拦截器的介绍

    Sturts2几个常用内建拦截器的介绍:1)conversation:这是一个处理类型转换错误的拦截器,它负责将类型转换错误从ActionContext中取出,并转换成Action的FieldErro ...

  2. vue之基础---组件基础

    (1)基本示例 Vue组件示例 /* 先注册组件,定义一个名为button-component的新组件 */ Vue.component('button-component',{ data:funct ...

  3. 用Docker实现nginx多端口

    一.安装docker 需要阿里的epel源,需要联网 [root@bogon ~]#yum -y install docker [root@bogon ~]#systemctl start docke ...

  4. 【原】thinkphp实现存储session至redis

    Thinkphp\Library\Think\Session\Driver中新建redis缓存文件:Redis.class.php Thinkphp\Common\function.php 中 fun ...

  5. 39页第3题 求x的n次幂

    /*计算x的n次幂*/ #include<stdio.h> main(void) { int i,n; double x,y; printf("Enter x:");/ ...

  6. React组件设计技巧

    React组件设计 组件分类 展示组件和容器组件 展示组件 容器组件 关注事物的展示 关注事物如何工作 可能包含展示和容器组件,并且一般会有DOM标签和css样式 可能包含展示和容器组件,并且不会有D ...

  7. git帮助网址

    http://rogerdudler.github.io/git-guide/index.zh.html https://help.github.com/

  8. OS X中crt中文乱码

    SecureCRT中显示乱码的话,可以去设置为UTF-8编码: Session Options->Terminal->Appearance->Character Encoding,设 ...

  9. 程序员如何在百忙中更有效地利用时间,如何不走岔路,不白忙(忙得要有效率,要有收获)-----https://www.cnblogs.com/JavaArchitect/p/9080484.html

    https://www.cnblogs.com/JavaArchitect/p/9080484.html 程序员如何在百忙中更有效地利用时间,如何不走岔路,不白忙(忙得要有效率,要有收获)

  10. Leetcode 115.不同的子序列

    不同的子序列 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(例 ...