[IOS笔记]字典转模型
来源:互联网 发布:cad椭圆指定数据怎么画 编辑:程序博客网 时间:2024/05/17 07:34
#import "ViewController.h"#import "Shop.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){// 加载数据// 1获取全路径 NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil]; self.gDataArr = [NSArray arrayWithContentsOfFile:dataPath];// 字典转模型// 创建临时数组 NSMutableArray *tempArray = [NSMutableArray array]; for (NSDictionary *dict in self.gDataArr) {// 创建shop对象 Shop *shop = [[Shop alloc] init]; shop.name = dict[@"name"]; shop.icon = dict[@"icon"]; //把模型装入数组 [tempArray addObject:shop]; } self.gDataArr = tempArray; } return _gDataArr;}- (void)viewDidLoad { [super viewDidLoad];}//添加到购物车- (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设置数据// NSDictionary *dict = self.gDataArr[index];// iconView.image = [UIImage imageNamed:dict[@"icon"]];// titleLabel.text = dict[@"name"]; Shop *shop = self.gDataArr[index]; iconView.image = [UIImage imageNamed:shop.icon]; titleLabel.text = shop.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笔记]字典转模型
- IOS字典转模型
- ios 字典转模型
- iOS 字典转模型
- iOS 字典转模型
- iOS 字典转模型
- ios-字典转模型
- IOS 学习 字典转模型
- IOS中的字典转模型
- iOS 字典转模型示例
- ios开发--字典转模型
- iOS runtime字典转模型
- IOS之字典转模型
- iOS 字典自动转模型
- iOS 数组/字典转模型
- iOS 字典转模型~方法
- iOS模型设计---字典转模型
- IOS开发UI字典转模型
- 事务
- 用Windows自带画图软件吸取色值
- 值传递、指针传递、引用传递的区别
- HTML基础学习三
- cv::rectangle()函数简要介绍
- [IOS笔记]字典转模型
- 刷题总结
- css实现背景图PC端/手机端自适应
- C语言数据类类型
- Tomcat部署
- URL和URI的区别
- SQL SERVER中 性能问题
- assimpt模型导入配置
- 知道前序序列和后序序列求二叉树的个数+大数