iOS 简易购物车页面的搭建

简单购物车页面的搭建


1.基础页面的搭建

  • 在storyboard的cell中创建控件并进行约束,继承自定义的AZWineCell

  • 将cell中的子控件和自定义的AZWineCell一一进行连线

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet AZWineButton *minusBtn;
  • 让商品的增加和删减按钮继承于自定义的按钮,实现自定义样式
-(void)awakeFromNib
{
    self.layer.borderWidth=1;
    self.layer.borderColor=[UIColor orangeColor].CGColor;
    self.layer.cornerRadius=self.frame.size.width*0.5;
}

2.加载模型数据

  • 这里使用懒加载的方式加载数据
-(NSMutableArray *)wineArray
{
    if (!_wineArray) {
        // 获得路径
        NSString *path=[[NSBundle mainBundle]pathForResource:@"wine.plist" ofType:nil];
        // 获得数组
        NSArray *array=[NSArray arrayWithContentsOfFile:path];
        // 创建一个临时数组存放模型数据
        NSMutableArray *tempArray=[NSMutableArray array];
        // 添加模型
        for (NSDictionary *dict in array) {
            //字典转模型
            AZWine *wine=[AZWine wineWithDict:dict];
            // 实现对wine模型内num值变化的监听
            [wine addObserver:self forKeyPath:@"num" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
            [tempArray addObject:wine];
        }
        _wineArray=tempArray;

    }
    return _wineArray;;
}
  • 给cell绑定模型数据,在模型的set方法给cell注入数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 绑定标识
    static NSString *ID=@"wine";
    // 创建cell
    AZWineCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    // 给cell注入数据
    cell.wine=self.wineArray[indexPath.row];
    // 返回cell
    return cell;
}
-(void)setWine:(AZWine *)wine
{
    _wine=wine;

    self.iconView.image=[UIImage imageNamed:wine.image];
    self.nameLabel.text=wine.name;
    self.priceLabel.text=wine.money;
    self.countLabel.text=[NSString stringWithFormat:@"%zd",wine.num];
    self.minusBtn.enabled=(wine.num>0);

}

3.设置代理,实现对按钮点击事件的监听

  • 自定义协议,提供协议方法供代理调用,监听按钮的点击
@protocol AZWineCellDelegate <NSObject>

@optional
/*增加商品按钮的点击*/
-(void)wineCellDidClickPlusButton:(AZWineCell *)cell;
/*删减商品按钮的点击*/
-(void)wineCellDidClickMinusButton:(AZWineCell *)cell;
@end

@interface AZWineCell : UITableViewCell
/*模型*/
@property(nonatomic,strong)AZWine *wine;
/*设置代理*/
@property(nonatomic, weak) id<AZWineCellDelegate> delegate;

@end
  • 修改模型数据,修改界面,通知代理实现协议方法
- (IBAction)minusClick {
    // 修改模型
    self.wine.num--;
    // 修改界面
    self.countLabel.text=[NSString stringWithFormat:@"%zd",self.wine.num];
    // 按钮是否可以点击
    if (self.wine.num==0) {
        self.minusBtn.enabled=NO;
    }
    // 通知代理
    if([self.delegate respondsToSelector:@selector(wineCellDidClickMinusButton:)]){
        [self.delegate wineCellDidClickMinusButton:self];
    }

}
- (IBAction)plusClick {
    // 修改模型
    self.wine.num++;
    // 修改界面
    self.countLabel.text=[NSString stringWithFormat:@"%zd",self.wine.num];
    // 按钮是否可以点击
    self.minusBtn.enabled=YES;
    // 通知代理
    if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {
        [self.delegate wineCellDidClickPlusButton:self];
    }
}
  • 实现协议方法,完成总价的刷新
-(void)wineCellDidClickPlusButton:(AZWineCell *)cell
{
    // 计算总价
    int totalPrice=self.totalPriceView.text.intValue+cell.wine.money.intValue;
    // 刷新界面
    self.totalPriceView.text=[NSString stringWithFormat:@"%d",totalPrice];
    // 购买按钮
    self.purchaseBtn.enabled=YES;
    // 购物车
    if (![self.shoppingList containsObject:cell.wine]) {
        [self.shoppingList addObject:cell.wine];
    }

}

shoppingList

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值