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,842評論 2 17
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,765評論 18 399
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,301評論 25 708
  • 禪與 Objective-C 編程藝術 (Zen and the Art of the Objective-C C...
    GrayLand閱讀 1,645評論 1 10
  • 啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦
    Kmoon_7753閱讀 144評論 0 0