美文网首页橙红科技有限公司平时收藏IOS累之用之
iOS购物车商品加减逻辑(附赠block块使用)

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

作者: smile丽语 | 来源:发表于2016-08-29 17:12 被阅读2344次

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


购物车加减商品.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;
    
}

相关文章

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

    购物车商品的加减效果展示如下: 这里面提供两种方案,均是MVC模式,本人建议第一种模式,代码更方便简洁;第二种代码...

  • iOS-2 Block

    block块 系列文章: iOS Block浅浅析 - 简书 iOS Block实现原理 iOS Block __...

  • 购物车动画与加减

    实现以下业务逻辑 购物车动画 购物车内加减 一、购物车动画 通过点击控制显示与隐藏,

    iOS Block存储域及循环引用

    系列文章:iOS Block概念、语法及基本使用iOS Block实现原理iOS Block __block说明符...

  • 一篇文章看懂iOS代码块Block

    一篇文章看懂iOS代码块Block 一篇文章看懂iOS代码块Block

  • iOS Block实现原理

    系列文章:iOS Block概念、语法及基本使用iOS Block __block说明符iOS Block存储域及...

  • iOS-Block的详解

    学习Block的感悟 一. iOS代码块Block 1.1 概述 代码块Block是苹果在iOS4开始引入的对C语...

  • vuejs仿美团,饿了么项目之——购物车篇

    上一篇写了个商品加减的组件,这篇说一下购物车。只有当添加商品的时候,也就是存在count这个key的时候,购物车的...

  • Block

    block.png iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实...

  • 微信小程序长按数量自动加减

    开发一个微信小程序,有购物车的功能,在测试加减购物车中商品数量时,发现点一下加一或减一太烦,于是添加了长按自动加减...

网友评论

  • LambZhou:第三步:在Controller里,使用代理,怎么使用吖?
  • 7538bd4d932a:挺好用
  • d496bf8fb8fe:有没有demo啊?
    smile丽语:@程小白 没有,但是建议你看第一种方案能自己写 demo 测试的

本文标题:iOS购物车商品加减逻辑(附赠block块使用)

本文链接:https://www.haomeiwen.com/subject/edbtettx.html