//
// XMGViewController.m,控制器类 #import "XMGViewController.h"
#import "XMGShop.h" @interface XMGViewController () // 购物车
@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 XMGViewController
/**
* 懒加载
*/
- (NSArray *)dataArr{
if (_dataArr == nil) {
// 加载数据
// 1.获取全路径
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil];//Supporting Files下面的shopData.plist文件。
self.dataArr = [NSArray arrayWithContentsOfFile:dataPath]; //dataPath = @"/Users/mctc/Library/Developer/CoreSimulator/Devices/4E7E6AB7-BB75-4C2C-9D87-21A0369A3DD6/data/Containers/Bundle/Application/A855282B-7D1A-450A-B3C3-8ACE93443577/03-综合练习.app/shopData.plist" 0x00007fafe150ef70
NSLog(@"%@",self.dataArr); //调用了get方法。
/*(
{
icon = danjianbao;
name = "\U5355\U5305";
},
{
icon = qianbao;
name = "\U94b1\U5305";
})*/ // 字典转模型对象
// 创建临时数组
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in _dataArr) {
// 创建shop对象
/*
// XMGShop *shop = [[XMGShop alloc] initWithIcon:dict[@"icon"] name:dict[@"name"]];
// XMGShop *shop = [XMGShop shopWithIcon:dict[@"icon"] name:dict[@"name"]];
// shop.name = dict[@"name"];
// shop.icon = dict[@"icon"];
*/
XMGShop *shop = [XMGShop shopWithDict:dict];
// 把模型装入数组
[tempArray addObject:shop];
}
self.dataArr = tempArray;
}
return _dataArr;
} // 初始化数据
- (void)viewDidLoad {
[super viewDidLoad];
/*类前缀
// Foundation
NSString;
NSArray;
NSDictionary;
NSURL; // UIKit
UIView;
UIImageView;
UISwitch;
*/
} /**
* 添加到购物车
*
* @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.设置数据*****************************/
// 设置数据
XMGShop *shop = self.dataArr[index];
iconView.image = [UIImage imageNamed:shop.icon];
titleLabel.text = shop.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
//
// XMGShop.h #import <Foundation/Foundation.h> @interface XMGShop : NSObject /** 图片的名称 */
@property (nonatomic, copy) NSString *icon;
/** 商品的名称 */
@property (nonatomic, copy) NSString *name; // 提供构造方法
/*
- (instancetype)initWithIcon: (NSString *)icon name: (NSString *)name;
+ (instancetype)shopWithIcon: (NSString *)icon name: (NSString *)name;
*/ - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)shopWithDict:(NSDictionary *)dict; @end
//
// XMGShop.m #import "XMGShop.h" @implementation XMGShop /*
- (instancetype)initWithIcon:(NSString *)icon name:(NSString *)name{
if (self = [super init]) {
self.icon = icon;
self.name = name;
}
return self;
} + (instancetype)shopWithIcon:(NSString *)icon name:(NSString *)name{
return [[self alloc] initWithIcon:icon name:name];
}
*/ /*构造方法,一般一共对象方法和类方法*/ - (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.name = dict[@"name"];
}
return self;
} + (instancetype)shopWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];//self alloc创建了一个类对象。
} @end

ios15--综合小例子的更多相关文章

  1. 前端小例子 基础js css html练习

    前情提要: 学前端也有一阵了,个人感觉前端还是重要的. html 学习教程 https://www.cnblogs.com/baili-luoyun/p/10466040.html css 教程 js ...

  2. c/c++ vector,map,set,智能指针,综合运用的小例子

    标准库,智能指针,综合运用的小例子 功能说明:查询单词在文件中出现的次数,如果在同一行出现多次,只算一次. 比如查询单词:你好 输出的结果: 你好 出现了:2次 (行号 2)xxxxxxx 你好 (行 ...

  3. 利用itext导出PDF的小例子

    我这边使用的jar包: itext-2.1.7.jar itextasian-1.5.2.jar 代码,简单的小例子,导出pdf: PdfService.java: package com.cy.se ...

  4. C# Newtonsoft.Json解析数组的小例子[转]

    https://blog.csdn.net/Sayesan/article/details/79756738 C# Newtonsoft.Json解析数组的小例子  http://www.cnblog ...

  5. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  6. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  7. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  8. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  9. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  10. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

随机推荐

  1. PHP实现写LOG日志的代码

    这篇文章给大家介绍的内容是关于PHP实现写LOG日志的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. public function write_log(){ //设置目录时间 ...

  2. 10.4 缓冲流 BufferedReader & BufferedWriter & 缓冲流特殊功能readLine

    缓冲流和正常流的使用大致相同,缓冲流效率更高. package day10_io_fileWrite_Read.buffer_stream; import java.io.*; /* * Buffer ...

  3. 使用Sophus练习李群SO3、SE3以及对应的李代数so3、se3

    这是高博<视觉SLAM14讲,从理论到实践>第4章的练习.加了一些注释和理解: #include <iostream>#include <cmath>using n ...

  4. Intel要在中国投35亿美金造这种闪存,3DxPoint技术牛在哪里?

    Repost: https://www.leiphone.com/news/201508/bbCUJqS2M3glCY3m.html 编者按: 今年的IDF上,Intel 再次强调了3DxPoint闪 ...

  5. JAVA基础——IO流字节流

    在Java中把不同的输入输出源(键盘.文件.网路连接)抽象表述为“流”. 1.输入流.输出流 .字节输入流通过FileInputStream和来操作 字节输出流通过FileOutputStream来操 ...

  6. Java基础——从数组到集合之间关键字的区别!!!!

    1.&& 和 &区别和联系: 相同点 : 结果是一样的.       不同点 :如果使用双&号判断,如果说条件一为false,不会判断条件二,但是单&号会继续判 ...

  7. cookie domain path 跨域

    1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...

  8. [Algorithm] 5. Kth Largest Element

    Description Find K-th largest element in an array. Example In array [9,3,2,4,8], the 3rd largest ele ...

  9. oracle打开或者关闭flashback

    1.打开flashback: 关闭数据库 SQL>shutdown immediate; 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为fla ...

  10. 三 , lnmp 一键包安装使用

    安装打包环境  #https://lnmp.org/----------------------------------------------------#安装wget -c http://soft ...