九宫格 - 购物车抽象实现

本文介绍了一个基于九宫格算法的简单购车购物车模型。该模型允许用户通过点击加号按钮向购物车添加商品,并可通过减号按钮移除商品。所有操作均在控制器内实现。

1.模拟一个简单的购车购物模型,运用到九宫格的算法,通过点击加号按钮往UIView(购物车)中添加商品

2.可以通过减号按钮将UIView的商品减少

3.这份代码添加商品/删除商品和九宫格算法的代码全部在控制器中实现,没有实现抽取

ViewController.m文件

#import "ViewController.h"

#import "ZHProducts.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutletUIView *shopCarView;

@property (weak, nonatomic) IBOutletUIButton *add;

@property (weak, nonatomic) IBOutletUIButton *remove;


@property (strong,nonatomic)NSArray *product;

@end


@implementation ViewController

//懒加载

-(NSArray *)product{

    if (_product ==nil) {

    /*

        _product = @[@{@"icon":@"danjianbao",@"title":@"单肩包"},

                     @{@"icon":@"shuangjianbao",@"title":@"双肩包"},

                     @{@"icon":@"xiekuabao",@"title":@"斜挎包"},

                     @{@"icon":@"qianbao",@"title":@"小钱包"},

                     @{@"icon":@"liantiaobao",@"title":@"链条包"},

                     @{@"icon":@"shoutibao",@"title":@"手提包"}

                     ];

        [_product writeToFile:@"/Users/admin/Desktop/product.plist" atomically:YES];

        */

        //获取全路径

        NSString *str = [[NSBundlemainBundle]pathForResource:@"product.plist"ofType:nil];

        //获取数据

        _product = [NSArrayarrayWithContentsOfFile:str];

        //定义一个临时的数组,用于接收遍历所得的数据

        NSMutableArray *arr = [NSMutableArrayarray];

        for (NSDictionary *dicin self.product) {

            

        ZHProducts *products = [[ZHProductsalloc]initWithProduct:dic];

            [arr addObject:products];

        }

        _product = arr;

    }

    return_product;

}


- (void)viewDidLoad {

    [superviewDidLoad];

}

//添加商品

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

    //************************************

    //定义列数

    NSInteger listNumber = 3;

    //定义imagebuttonweithheight

    CGFloat weith = 80;

    CGFloat height = 100;

    //计算shopimageview中的imagebutton

    NSInteger index =self.shopCarView.subviews.count;

    //求出水平间距和垂直间距

    CGFloat xGap = (self.shopCarView.frame.size.width - listNumber * weith)/(listNumber - 1);

    CGFloat yGap = (self.shopCarView.frame.size.height - (index / listNumber + 1)* height);

    //求出xy

    CGFloat x = (weith + xGap)*(index % listNumber);

    CGFloat y = (height +yGap)*(index / listNumber);

    

    //*********************创建button装入shopCarView**************

    UIButton *imageButton = [[UIButtonalloc]initWithFrame:CGRectMake(x, y, weith, height)];

    //设置imageButton的背景颜色

    imageButton.backgroundColor = [UIColorredColor];

    //将创建好的imageButton加到shopcarView

    [self.shopCarViewaddSubview:imageButton];

    

    //*********************imageButton中增加图片和标题**************

    //1.创建需要增加的图片对象

    UIImageView *imageView = [[UIImageViewalloc]init];

    imageView.frame = CGRectMake(0, 0, weith, weith);

    imageView.backgroundColor = [UIColorblueColor];

    [imageButton addSubview:imageView];

    //2.创建需要增加的标题对象

    UILabel *titleLabel = [[UILabelalloc]init];

    titleLabel.frame = CGRectMake(0, weith, weith, height - weith);

    titleLabel.backgroundColor = [UIColorpurpleColor];

    titleLabel.textAlignment =NSTextAlignmentCenter;

    [imageButton addSubview:titleLabel];

    

    //3.赋值数据

    ZHProducts *s = self.product[index];

    imageView.image = [UIImageimageNamed:s.icon];

    titleLabel.text = s.title;

    

    //*********************判断按钮状态条件**************

    sender.enabled = index!=5;

    if (index == 0) {

    self.remove.enabled =YES;

    }

}

//移除商品

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

    NSInteger index =self.shopCarView.subviews.count;

    UIImageView *view = [self.shopCarView.subviewslastObject];

    [view removeFromSuperview];

        self.add.enabled =YES;

        sender.enabled = index !=1;

}


@end


//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值