购物车商品的加减效果展示如下:
这里面提供两种方案,均是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;
}