//
// ViewController.m
// 03-综合练习
//
#import "ViewController.h"
@interface ViewController ()
// 购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarView;
// 添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
// 删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton;
// 全局的下标
//@property (nonatomic, assign) NSInteger index;
@implementation ViewController
/**
* 添加到购物车
*
* @param button 按钮
*/
/***********************2.创建一个商品*****************************/
// 1.创建商品的view
UIView *shopView = [[UIView alloc] init];
// 2.设置frame
shopView.frame = CGRectMake(x, y, width, height);
// 3.设置背景颜色
shopView.backgroundColor = [UIColor greenColor];
// 4.添加到购物车
[self.shopCarView addSubview:shopView];
// 5.创建商品的UIImageView对象
UIImageView *iconView = [[UIImageView alloc] init];
iconView.frame = CGRectMake(, , width, width);
iconView.backgroundColor = [UIColor blueColor];
[shopView addSubview:iconView];
// 6.创建商品标题对象
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.frame = CGRectMake(, width, width, height - width);
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
[shopView addSubview:titleLabel];
/***********************3.设置数据*****************************/
// 数值数据
/*
// 方式一: (不可取:数据都是一样)
iconView.image = [UIImage imageNamed:@"danjianbao"];
titleLabel.text = @"单肩包";
*/
// 方式二 (不可取:太冗余)
/\*
if (index == 0) {
iconView.image = \[UIImage imageNamed:@"danjianbao"\];
titleLabel.text = @"单肩包";
}else if (index == 1){
iconView.image = \[UIImage imageNamed:@"qianbao"\];
titleLabel.text = @"钱包";
}else if (index == 2){
iconView.image = \[UIImage imageNamed:@"danjianbao"\];
titleLabel.text = @"单肩包";
}else if (index == 3){
iconView.image = \[UIImage imageNamed:@"danjianbao"\];
titleLabel.text = @"单肩包";
}else if (index == 4){
iconView.image = \[UIImage imageNamed:@"danjianbao"\];
titleLabel.text = @"单肩包";
}else if (index == 5){
iconView.image = \[UIImage imageNamed:@"danjianbao"\];
titleLabel.text = @"单肩包";
}
\*/
// 方式三 (数组: (两个数组之间没有任何联系,容易出错))
/\*
NSArray<NSString \*> \*imageNames = @\[@"danjianbao", @"qianbao", @"liantiaobao", @"shoutibao", @"shuangjianbao", @"xiekuabao"\];
NSArray<NSString \*> \*titleNames = @\[@"单肩包", @"钱包", @"链条包", @"手提包", @"双肩包", @"斜挎包"\];
// 设置数据
iconView.image = \[UIImage imageNamed:imageNames\[index\]\];
titleLabel.text = titleNames\[index\];
\*/
// 方式四 (数组 + 字典)
NSArray<NSDictionary \*> \*dataArr = @\[
@{@"name":@"单肩包", @"icon":@"danjianbao"},
@{@"name":@"钱包", @"icon":@"qianbao"},
@{@"name":@"链条包", @"icon":@"liantiaobao"},
@{@"name":@"手提包", @"icon":@"shoutibao"},
@{@"name":@"双肩包", @"icon":@"shuangjianbao"},
@{@"name":@"斜挎包", @"icon":@"xiekuabao"}
\];
// 设置数据
NSDictionary \*dict = dataArr\[index\];
iconView.image = \[UIImage imageNamed:dict\[@"icon"\]\];
titleLabel.text = dict\[@"name"\];
/***********************4.设置按钮的状态*****************************/
button.enabled = (index != );
// 5.设置删除按钮的状态
self.removeButton.enabled = YES;
}
/**
* 从购物车中删除
*
* @param button 按钮
*/
(IBAction)remove:(UIButton *)button {
// 1. 删除最后一个商品
UIView *lastShopView = [self.shopCarView.subviews lastObject];
[lastShopView removeFromSuperview];
// 3. 设置添加按钮的状态
self.addButton.enabled = YES;
// 4. 设置删除按钮的状态
/*
if (self.shopCarView.subviews.count == 0) {
self.removeButton.enabled = NO;
}
*/
self.removeButton.enabled = (self.shopCarView.subviews.count != );
}
@end
手机扫一扫
移动阅读更方便
你可能感兴趣的文章