iOS搭建简易购物车页面

来自:网络
时间:2023-07-25
阅读:

本文实例为大家分享了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];
    }

}

iOS搭建简易购物车页面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

返回顶部
顶部