字典转成模型

 导入模型

创建模型

 

继承于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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值