iOS购物车商品加减逻辑(附赠block块使用)

购物车商品的加减效果展示如下:

这里面提供两种方案,均是MVC模式,本人建议第一种模式,代码更方便简洁;第二种代码里着重强调block块使用,自行看代码理解

##一、第一种方案是用代理和枚举值 仅仅抽取关键性代码,使用如下: ###第一步:在Model里,将需要的属性自定义

// 标题
@property (nonatomic, copy) NSString *title;

// 价格
@property (nonatomic, copy) NSString *price;

// 是否选中
@property (nonatomic, assign, getter=isSeleted) BOOL selected;

// 选择商品数量值
@property (nonatomic, assign) NSInteger number;

// 商品总价
@property (nonatomic, assign, readonly) double totalPrice;
复制代码

###第二步:在View里,使用枚举将选择及加减商品区分,采用代理模式进行回调使用

/**
 商品数量变化的枚举值
 */
typedef enum : NSUInteger {
    ShopNmberChangeAdd,//加
    ShopNmberChangeMin,//减
    ShopNmberChangeSelected,//选择
} ShopNmberChange;

@protocol YYPCartCellDelegate <NSObject>

- (void)CartCell:(YYPCartCell *)cell addChangeNumberOfShop:(ShopNmberChange)change;

@end


@interface YYPCartCell : UITableViewCell

@property (strong, nonatomic) YYPGoodsModel *model;

// 创建并返回cell
+ (instancetype)cellWithTableView:(UITableView *)tableView;

@property (nonatomic, weak) id<YYPCartCellDelegate> delegate;

@end
复制代码
// 选中按钮点击事件
- (void)selectBtnClick:(UIButton*)button {
    
    button.selected = !button.selected;
    self.model.selected = button.selected;
    if ([self.delegate respondsToSelector:@selector(CartCell:addChangeNumberOfShop:)]) {
        [self.delegate CartCell:self addChangeNumberOfShop:ShopNmberChangeSelected];
        
    }
}

- (void)changeNumberClick:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(CartCell:addChangeNumberOfShop:)]) {
        if ([btn isEqual:self.addBtn]) {
            self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)++self.model.number];
            [self.delegate CartCell:self addChangeNumberOfShop:ShopNmberChangeAdd];
        } else {
            self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)--self.model.number];
            [self.delegate CartCell:self addChangeNumberOfShop:ShopNmberChangeMin];
        }
    }
}
复制代码

###第三步:在Controller里,使用代理

- (void)setModel:(YYPGoodsModel *)model {
    _model = model;
    
    if ([self.dataArray containsObject:model]) {
        model.number++;
    } else {
        [self.dataArray insertObject:model atIndex:0];
    }
    
}
复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYPCartCell *cell =[YYPCartCell cellWithTableView:tableView];

    cell.model = self.dataArray[indexPath.row];
    cell.delegate = self;
    
    return cell;
}
复制代码

##二、第二种方案是在cell里我们采用block块回调

Block 回调自行理解,代码如下:

/**
 *
 *  cell是否被选中的回调
 *
 *  @param select 是否被选中
 */
typedef void(^YYPCartBlock)(BOOL select);

/**
 *
 *  数量改变的回调
 */
typedef void(^YYPNumChange)();

//数量显示
@property (nonatomic, retain)UILabel *numberLabel;
@property (nonatomic, assign)BOOL isSelected;
@property (nonatomic, copy)YYPCartBlock cartBlock;
@property (nonatomic, copy)YYPNumChange numAddBlock;
@property (nonatomic, copy)YYPNumChange numCutBlock;
复制代码
// 选中按钮点击事件
- (void)selectBtnClick:(UIButton*)button {
    
    button.selected = !button.selected;
    if (self.cartBlock) {
        self.cartBlock(button.selected);
    }
}

// 数量加按钮
- (void)addBtnClick {
    
    if (self.numAddBlock) {
        self.numAddBlock();
    }
}

// 数量减按钮
-(void)cutBtnClick {
    
    if (self.numCutBlock) {
        self.numCutBlock();
    }
}
复制代码

在控制器里,数据源方法去使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYPCartCell *cell =[YYPCartCell cellWithTableView:tableView];

    cell.isSelected = self.isSelectAll;
    
    //是否被选中
    if ([self.selectGoods containsObject:[self.dataArray objectAtIndex:indexPath.row]]) {
        cell.isSelected = YES;
    }
    
    // 选择回调
    cell.cartBlock = ^(BOOL isSelec){
        
        if (isSelec) {
            [self.selectGoods addObject:[self.dataArray objectAtIndex:indexPath.row]];
        } else {
            [self.selectGoods removeObject:[self.dataArray objectAtIndex:indexPath.row]];
        }
        
        if (self.selectGoods.count == self.dataArray.count) {
            self.selectAll.selected = YES;
        } else {
            self.selectAll.selected = NO;
        }
        
        [self countPrice];
    };
    
    __block YYPCartCell *weakCell = cell;
    cell.numAddBlock =^(){
        
        NSInteger count = [weakCell.numberLabel.text integerValue];
        count++;
        NSString *numStr = [NSString stringWithFormat:@"%ld",(long)count];
        
        YYPGoodsModel *model = [self.dataArray objectAtIndex:indexPath.row];
        
        weakCell.numberLabel.text = numStr;
        model.number = count;
        
        [self.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
        if ([self.selectGoods containsObject:model]) {
            [self.selectGoods removeObject:model];
            [self.selectGoods addObject:model];
            [self countPrice];
        }
    };
    
    cell.numCutBlock =^(){
        
        NSInteger count = [weakCell.numberLabel.text integerValue];
        count--;
        if(count < 0){
            return ;
        }
        NSString *numStr = [NSString stringWithFormat:@"%ld",(long)count];
        
        YYPGoodsModel *model = [self.dataArray objectAtIndex:indexPath.row];
        
        weakCell.numberLabel.text = numStr;
        
        model.number = count;
        [self.dataArray replaceObjectAtIndex:indexPath.row withObject:model];
        
        //判断已选择数组里有无该对象,有就删除  重新添加
        if ([self.selectGoods containsObject:model]) {
            [self.selectGoods removeObject:model];
            [self.selectGoods addObject:model];
            [self countPrice];
        }
    };
    
    [cell reloadDataWith:[self.dataArray objectAtIndex:indexPath.row]];
    
    return cell;
    
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值