导入模型
创建模型
继承于Nsobject
//
// SHopp.h
// Gouwuche
//
// Created by imac on 2019/6/29.
// Copyright © 2019 imac. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SHopp : NSObject
/** 图片 */
@property (nonatomic,copy) NSString * icon;
/** 商品名称 */
@property (nonatomic,copy) NSString * name;
@end
NS_ASSUME_NONNULL_END
//
// ViewController.m
// Gouwuche
//
// Created by imac on 2019/5/18.
// Copyright © 2019 imac. All rights reserved.
//
#import "ViewController.h"
#import "SHopp.h"
@interface ViewController ()
//购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarViw;
//添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addbutton;
//删除按钮
@property (weak, nonatomic) IBOutlet UIButton *deletebutton;
//数据数组
@property (nonatomic,strong) NSArray *dataArr;
@end
@implementation ViewController
/**
懒加载
作用:
用到才加载
全局只会加载一次
全局都可以使用
*/
/**
过程
1.重写成员的get方法
2 在get方法中判断
1如果为空 加载数据
2〉如果不为空 直接返回数据
*/
//相当于重写get方法
-(NSArray *)dataArr{
if ( _dataArr == nil) {
//加载数据获取全路径 字典替换成。plist文件
NSString *datapath=[[NSBundle mainBundle ]pathForResource:@"shopArray.plist" ofType:nil];
//arrayWithContentsOfFile 全路径
self.dataArr=[NSArray arrayWithContentsOfFile:datapath];
//字典转模型
//创建临时数组
NSMutableArray * tempArray= [NSMutableArray array];
for (NSDictionary * dit in self.dataArr) {
SHopp *shopp = [[SHopp alloc] init];
shopp.name=dit[@"name"];
shopp.icon=dit[@"icon"];
//把模型装入数组
[tempArray addObject:shopp];
}
self.dataArr=tempArray;
// self.dataArr =@ [
// @{@"name":@"单肩包",@"icon":@"a"},
// @{@"name":@"双肩包",@"icon":@"jian"},
// @{@"name":@"斜挎包",@"icon":@"jian"},
// @{@"name":@"LV包",@"icon":@"a"},
// @{@"name":@"手术包",@"icon":@"a"},
// @{@"name":@"二级包",@"icon":@"a"}
// ];
}
return _dataArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 给index 赋值
// self.index=0;
// 裁剪多余部分(不可取)
//self.shopCarViw.clipsToBounds= YES;
}
//添加购物车
- (IBAction)Add:(UIButton *)button {
//***********************1.定义一些常量**************************
//1.总列数
NSInteger allclns=3;
//2.商品的宽
CGFloat with=80;
CGFloat height=100;
//3.求出水平间距
CGFloat hMargin= (self.shopCarViw.frame.size.width-allclns*with)/(allclns-1);
CGFloat vMargin= (self.shopCarViw.frame.size.height-2*height)/1;
//4. 设置索引‘
NSInteger index=self.shopCarViw.subviews.count;
//4.求出X值
CGFloat x=(hMargin + with)*(index % allclns);
CGFloat y=(vMargin + height)*(index /allclns);
//***********************2.创建商品**************************
// 1 创建商品的View
UIView * shopView = [[UIView alloc] init];
//2设置freamm
shopView.frame =CGRectMake(x, y, 80,100 );
//3设置背景颜色
shopView.backgroundColor=[UIColor greenColor];
//4添加到购物车
[self.shopCarViw addSubview:shopView];
//5.创建商品 IUIImageView
UIImageView * iconView =[[UIImageView alloc] init];
iconView.frame=CGRectMake(0, 0, with, with);
iconView.backgroundColor=[UIColor redColor];
[shopView addSubview:iconView];
//6.创建商品 的标签 IUILabel
UILabel * titlelabel =[[UILabel alloc] init];
titlelabel.frame=CGRectMake(0, with, with, height-with);
titlelabel.backgroundColor=[UIColor yellowColor];
titlelabel.textAlignment=NSTextAlignmentCenter;
[shopView addSubview:titlelabel];
//设置 数据
/**懒加载 */
// if (self.dataArr == nil) {
// self.dataArr =@ [
// @{@"name":@"单肩包",@"icon":@"a"},
// @{@"name":@"双肩包",@"icon":@"jian"},
// @{@"name":@"斜挎包",@"icon":@"jian"},
// @{@"name":@"LV包",@"icon":@"a"},
// @{@"name":@"手术包",@"icon":@"a"},
// @{@"name":@"二级包",@"icon":@"a"}
// ];
// }
//NSDictionary *dict=self.dataArr[index];
SHopp *shopp =self.dataArr[index];
//iconView.image=[UIImage imageNamed:dict[@"icon"]];
iconView.image=[UIImage imageNamed:shopp.icon];
// titlelabel.text=dict[@"name"];
titlelabel.text=shopp.name;
//***********************3.判断按钮状态**************************
// if(index == 5){
// button.enabled=NO;
// }
button.enabled=(index!=5);
//***********************5.设置删除按钮状态**************************
self.deletebutton.enabled=YES;
//给下标加1
//self.index++;
}
//删除购物车
- (IBAction)delete:(UIButton *)button{
// 1.删除最后一个商品
UIView * lastshopView =[self.shopCarViw.subviews lastObject];
[lastshopView removeFromSuperview];
//2.设置索引值 -1
//self.index-=1;
//3,设置a添加按钮的状态
self.addbutton.enabled=YES;
//4,设置删除按钮的状态
// if(self.shopCarViw.subviews.count== 0){
// self.deletebutton.enabled=NO;
// }
self.deletebutton.enabled=(self.shopCarViw.subviews.count!= 0);
}
@end