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

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


购物车加减商品.gif

这里面提供两种方案,均是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;
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,927评论 2 17
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,803评论 18 399
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,932评论 25 709
  • 禅与 Objective-C 编程艺术 (Zen and the Art of the Objective-C C...
    GrayLand阅读 1,671评论 1 10
  • 啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦
    Kmoon_7753阅读 144评论 0 0