[IOS笔记]懒加载—购物车加载数组和字典
来源:互联网 发布:qq空间营销软件 编辑:程序博客网 时间:2024/05/18 04:51
//// ViewController.m// ShoppingCart//// Created by cdj on 17/9/8.// Copyright © 2017年 ue. 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 *gDataArr;@end@implementation ViewController-(NSArray *)gDataArr{ if(_gDataArr == nil){// 加载数据 _gDataArr = @[ @{@"name":@"单肩包", @"icon":@"danjianbao"}, @{@"name":@"钱包", @"icon":@"qianbao"}, @{@"name":@"链条包", @"icon":@"liantiaobao"}, @{@"name":@"手提包", @"icon":@"shoutibao"}, @{@"name":@"双肩包", @"icon":@"shuangjianbao"}, @{@"name":@"斜挎包", @"icon":@"xiekuabao"}]; } return _gDataArr;}/* 懒加载: 1作用:(1)用到的时候再加载;(2)全局只被加载一次 (3)全局都能使用 2过程:(1)重写成员变量的get方法;(2)再get方法中判断:如果为空酒加载数据,否则直接返回数据 */- (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.gDataArr = dataArr; */}//添加到购物车- (IBAction)add:(UIButton *)button { // 1.总列数 NSInteger allCols = 3;// 2.商品的宽度 CGFloat width = 80; CGFloat height = 100;// 3.求水平间距 CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width)/(allCols -1); CGFloat vMargin = (self.shopCarView.frame.size.height - 2 * height)/1;// 4.用subview的个数代替索引 NSInteger index = self.shopCarView.subviews.count; // 4.求出x值 CGFloat x = (hMargin + width) * (index % allCols); CGFloat y = (vMargin + height) * (index / allCols); // 5创建商品的view UIView *shopView = [[UIView alloc] init]; shopView.frame = CGRectMake(x, y, width, height); shopView.backgroundColor = [UIColor greenColor]; [self.shopCarView addSubview:shopView]; // 6创建商品的UIImageView对象 UIImageView *iconView = [[UIImageView alloc] init]; iconView.frame = CGRectMake(0, 0, width, width); iconView.backgroundColor = [UIColor purpleColor]; [shopView addSubview:iconView]; // 7创建商品的label对象 UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.frame = CGRectMake(0, width, width, height - width); titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; [shopView addSubview:titleLabel]; // 8设置数据// 方式一// iconView.image = [UIImage imageNamed:@"danjianbao"];// titleLabel.text = @"单肩包"; // 方式二 if else if// 方式三 数组// 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"]; // 方式五// NSDictionary *dict = self.gDataArr[index];// iconView.image = [UIImage imageNamed:dict[@"icon"]];// titleLabel.text = dict[@"name"]; // 方式六:懒加载 NSDictionary *dict = self.gDataArr[index]; iconView.image = [UIImage imageNamed:dict[@"icon"]]; titleLabel.text = dict[@"name"]; // 设置按钮的状态 if (index == 5) { button.enabled = NO; } // 删除按钮的状态 self.removeButton.enabled = YES; }//从购物车移除- (IBAction)remove:(UIButton *)button { // 1删除最后一个商品 UIView *lastShowView = [self.shopCarView.subviews lastObject]; [lastShowView removeFromSuperview]; // 2设置索引的值 self.addButton.enabled = YES; if (self.shopCarView.subviews.count == 0) { self.removeButton.enabled = NO; } }@end
阅读全文
0 0
- [IOS笔记]懒加载—购物车加载数组和字典
- 懒加载plist文件——格式(数组,字典,数组,字典)
- iOS学习笔记:懒加载
- iOS懒加载学习笔记
- 网络加载购物车布局
- IOS学习笔记---文件加载和保存
- Retrofit和Rxjava配合使用加载购物车
- IOS学习之——字典转模型 加载plist
- iOS学习笔记--懒加载(延迟加载)
- ios学习笔记(五)数组和字典
- ios学习笔记——懒加载的使用
- iOS 学习笔记之懒加载
- iOS—懒加载模式
- 网络加载购物车自定义算价格
- iPhone开发之字典转模型——利用创建plist文件和懒加载
- IOS xib,文件加载笔记
- 【代码笔记】iOS-正在加载
- 懒加载-可变数组
- 顺序表
- 设计模式
- 解决使用offsetWidth让DIV自动变宽时出现的小BUG。
- backdoorppt:一个可将载荷伪装成PPT的工具
- mnist svm
- [IOS笔记]懒加载—购物车加载数组和字典
- html超链接
- beyond compare 找不到靠谱“绿色版”怎么办
- Redis内存使用优化与存储
- python argparse 用法总结
- hdu 1217 # 最短路
- Mac本下Oracle InstantClient的安装(sqlplus命令不能执行的解决方案)
- UVALive3983[Robotruck] 动态规划 滑动窗口优化
- java Date日期格式转换