用OC和UI实现购物车功能 在iOS平台上

购物车项目

作用:可以通过按钮把物品简单的添加到购物车中

思路:

一,设置两个加减按钮,有普通,高亮和enable状态,在interface中有属性声明

二,设置购物车的imageview,在interface中有属性声明

三,设置数组装载字典对象,字典里包含物品图片的名字和物品标题,写入一个plist文件中,然后放到singel view application的support file中

四,创建两个类,分别是Shop(继承于NSObject)和ShopView(继承于UIView)。Shop用于从数组获取图片的名字和标题;ShopView用于设置图片的frame和标题。

五,设置九宫格,用添加的图片个数作为索引,计算出每个图片的距离以及需要换行的数量.

大致如图

-----------------------------------LHJViewController.m中代码实现------------------------------------

//

//  LHJViewController.m

//  购物车

//

//  Created by 梁华建 on 2017/9/19.

//  Copyright © 2017 梁华建. All rights reserved.

//

#import "LHJShop.h"

#import "LHJViewController.h"

@interface LHJViewController ()

//按钮的对象

@property (weak, nonatomic) IBOutlet UIButton *Subtrack;

@property (weak, nonatomic) IBOutlet UIButton *AddButton;

//购物车的image

@property (weak, nonatomic) IBOutlet UIImageView *UICarImage;

@property(nonatomic,strong)NSArray *Arrdic;

@end


@implementation LHJViewController


/********************加载数据进入数组**********************/

-(NSArray *)Arrdic

{

    if (_Arrdic==nil) {

     //plist文件中读取字典

    NSString *PathForPlist=[[NSBundle mainBundle]pathForResource:@"Arr.plist" ofType:nil];

    self.Arrdic=[NSArray arrayWithContentsOfFile:PathForPlist];

    //创建可变数组装载Arrdic

   NSMutableArray *dict=[[NSMutableArray alloc]init];

    //遍历

    for (NSDictionary *dic in self.Arrdic) {

        LHJShop *shop1=[[LHJShop alloc]initWithDictionary:dic];

        [dict addObject:shop1];

     }

    //把可变数组装进去

    self.Arrdic=dict;

                 }

    return  _Arrdic;

}

- (void)viewDidLoad {

    [super viewDidLoad];

}

//加入购物车按钮

- (IBAction)AddToCar:(UIButton *)sender {

    //设置图片数量

    NSInteger num=self.UICarImage.subviews.count;

    /******************设置九宫格******************/

    //列数

    NSInteger list=3;

    //图片的xy

    CGFloat xwidht=80;

    CGFloat yheight=100;

    //计算水平间距和垂直间距

    CGFloat xw=(self.UICarImage.frame.size.width-list*xwidht)/3;

    CGFloat yh=(self.UICarImage.frame.size.height-2*yheight)/1;

    //求出xy的值

    CGFloat x=(xwidht+xw)*(num%list);

    CGFloat y=(yheight+yh)*(num/list);


    /**************添加购物车图****************/

    //  取出数据

    LHJShop *Shop=self.Arrdic[num];

     LHJShopView *LHJshopview=[[LHJShopView alloc]initWithShop:Shop];

    LHJshopview.frame=CGRectMake(x,y, xwidht, yheight);

    [self.UICarImage addSubview:LHJshopview];

    /*-----------------读取数据------------------*/

//设置按钮状态

    self.AddButton.enabled=(num!=5);

    self.Subtrack.enabled=YES;

}

//去除购物车按钮

- (IBAction)SubtractToCar:(UIButton *)sender{

   UIView *lastobject= [self.UICarImage.subviews lastObject];

    [lastobject removeFromSuperview];

    self.AddButton.enabled=YES;

    self.Subtrack.enabled=(self.UICarImage.subviews.count!=0);

}

@end

----------------------------shop中代码(获取字典中的数据)--------------------------

//  LHJShop.h


#import <Foundation/Foundation.h>


@interface LHJShop : NSObject

/** 图片的名称*/

@property(nonatomic,copy)NSString *name;

/** 商品的名称*/

@property(nonatomiccopy)NSString *icon;

-(instancetype)initWithDictionary:(NSDictionary *)dic;

+(instancetype)ArrayWithDictionary:(NSDictionary *)dic;

@end


//  LHJShop.m


#import "LHJShop.h"


@implementation LHJShop

//设置类工厂和initwith函数 获得并封装字典中的图片名字和图片标题

-(instancetype)initWithDictionary:(NSDictionary *)dic

{

    LHJShop *shop=[[LHJShop alloc ]init];

    shop.name=dic[@"name"];

    shop.icon=dic[@"icon"];

    return shop;

}


+(instancetype)ArrayWithDictionary:(NSDictionary *)dic

{

    return [[LHJShop alloc]initWithDictionary:dic];

}

@end






-------------------------ShopView(加载图片)------------------------------

//  LHJShopView.h


@class LHJShop;

#import <UIKit/UIKit.h>

//自定义控件

//init初始化子控件

//layoutSubviews中设置frame

//在外面设置数据

@interface LHJShopView : UIView

@property(nonatomic,strong)LHJShop *Shop;

-(instancetype)initWithShop:(LHJShop *)shop;

+(instancetype)LHJShopViewWithShop:(LHJShop *)shop;


@end



//  LHJShopView.m


#import "LHJShopView.h"

#import "LHJShop.h"

@interface LHJShopView ()

//设置图片

@property(nonatomic,weak)UIImageView *imageview;

//设置文字

@property(nonatomic,weak)UILabel *label;

@end


@implementation LHJShopView

-(instancetype)init

{

//设置图片

    if(self=[super init])

    {

        [self SetUp];

    }

    return self;

}

//定义布局 (设置frame

-(void)layoutSubviews

{

    [super layoutSubviews];

    CGFloat xwidht=self.frame.size.width;

    CGFloat yheight=self.frame.size.height;

    self.imageview.frame=CGRectMake(0,0,xwidht,xwidht);

    self.label.frame=CGRectMake(0,xwidht,xwidht,yheight-xwidht);

//    NSLog(@"%f------%f",xwidht,yheight);

}


+(instancetype)LHJShopViewWithShop:(LHJShop *)shop

{

    return  [[self alloc]initWithShop:shop];

}

-(instancetype)initWithShop:(LHJShop *)shop

{

    

    if (self=[super init]) {

        [self SetUp];

        self.Shop=shop;

    }

    return self;

}

-(void)setShop:(LHJShop *)Shop

{

    _Shop=Shop;

   self.imageview.image=[UIImage imageNamed:Shop.name];

       self.label.text=Shop.icon;

 

}

-(void)SetUp

{

    //设置imageview

    UIImageView *imageview=[[UIImageView alloc]init];

    imageview.backgroundColor=[UIColor yellowColor];

    [self addSubview:imageview];

    _imageview=imageview;

    //设置label

    UILabel *label=[[UILabel alloc]init];

    label.textAlignment=NSTextAlignmentCenter;

    label.backgroundColor=[UIColor greenColor];

      [self addSubview:label];

    _label=label;


}

@end

-------------------------------------------------------------------
总结:
难点:自定义控件
为了封装好数据,在内部加载数据,要清楚的知道传了什么数据去什么地方,还有getter或者setter方法的重写不要写成死循环,整理好思路。
实在找不出问题就打断点,在每一个传参后面用NSLog函数打印一下信息,判断哪里数据转换失败,本人就因为写错一个setter方法搞了了比较长的时间。




猜你喜欢

转载自blog.csdn.net/MChuajian/article/details/78072022