优雅的实现商城类APP购物车总价的计算----block
2017-02-22 本文已影响89人
Vesincc
废话不多说,先上图

这么一个小小的cell上面TMD居然有3个可以点的地方,而且点击整个cell还要跳转到详情页面,看来tableview的didselec方法是无法满足我们的需求了。
先不管那么多,把cell做出来再bibi,根据复用性原则,那个可加可减的按钮,先包起来做成一个view,剩下左边那个直接一个按钮搞定。
cell做好了,那该怎么计算呢,真是头大。网上找了一堆方法,大多数都说设置tag,再加不同的点击事件。但是对于强迫症来说,用了tag之后,自己的代码怎么看怎么别扭,难道就没有一种更优雅的方式来实现了吗? 答案是肯定的,当然有,而且还不止一种,block、delegate。
让我们来用block实现一哈。首先带加减的按钮要能够控制model中的数量,这个view肯定要先传一个block下去。让我们来看一下这个view的实现
.h
#import <UIKit/UIKit.h>
typedef void (^numberChange_Block)(NSString *number);
@interface SelectButtonView : UIView
@property (nonatomic, copy) numberChange_Block block;
@property (nonatomic, strong) NSString *optionData;
@property (nonatomic, strong) UIButton *addButton;
@property (nonatomic, strong) UIButton *lessButton;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, assign) NSInteger num;
- (void)NumAdd;
- (void)NumLess;
- (void)setItemNum:(NSInteger)num;
- (NSInteger)getNum;
@end
.m
#import "SelectButtonView.h"
@implementation SelectButtonView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.num = 1;
self.layer.borderWidth = 0.5;
self.layer.borderColor = UIColorFromRGB(0xf1f1f1).CGColor;
self.layer.cornerRadius = 4;
self.lessButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45*ScaleSize, 45*ScaleSize)];
self.addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45*ScaleSize, 45*ScaleSize)];
[self.lessButton setTitle:@"—" forState:UIControlStateNormal];
[self.addButton setTitle:@"+" forState:UIControlStateNormal];
[self.lessButton setTitleColor:UIColorFromRGB(0x3a3a3a) forState:UIControlStateNormal];
[self.addButton setTitleColor:UIColorFromRGB(0x3a3a3a) forState:UIControlStateNormal];
self.lessButton.titleLabel.font = [UIFont systemFontOfSize:14];
self.addButton.titleLabel.font = [UIFont systemFontOfSize:18];
self.addButton.right = self.right;
[self addSubview:self.lessButton];
[self addSubview:self.addButton];
self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.addButton.width, 0, self.width-self.addButton.width - self.lessButton.width, self.height)];
self.numLabel.textAlignment = NSTextAlignmentCenter;
self.numLabel.text = [NSString stringWithFormat:@"%ld", self.num];
self.numLabel.textColor = UIColorFromRGB(0x3a3a3a);
self.numLabel.font = [UIFont defaultFontOfSize:12];
[self addSubview:self.numLabel];
UIView *line1 = [Tools aLineViewWithPosition:CGRectMake(0, 0, 0.5, self.height)];
line1.centerX = self.lessButton.right;
[self addSubview:line1];
UIView *line2 = [Tools aLineViewWithPosition:CGRectMake(0, 0, 0.5, self.height)];
line2.centerX = self.addButton.left;
[self addSubview:line2];
[self.addButton addTarget:self action:@selector(NumAdd) forControlEvents:UIControlEventTouchUpInside];
[self.lessButton addTarget:self action:@selector(NumLess) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)NumAdd {
self.num++;
self.numLabel.text = [NSString stringWithFormat:@"%ld", self.num];
self.block([NSString stringWithFormat:@"%ld", self.num]);
}
- (void)NumLess {
if (self.num != 1) {
self.num--;
self.numLabel.text = [NSString stringWithFormat:@"%ld", self.num];
self.block([NSString stringWithFormat:@"%ld", self.num]);
}
}
- (void)setItemNum:(NSInteger)num {
self.num = num;
self.numLabel.text = [NSString stringWithFormat:@"%ld", self.num];
}
- (NSInteger)getNum {
return self.num;
}
这样的话,我们只需要在cell赋值的时候顺便把block也丢进来,不就可以达到效果了吗,试一下 果然可以。同样,cell上面的button也这样来搞。
.h
#import <UIKit/UIKit.h>
#import "SelectButtonView.h"
#import "GoodsModel.h"
typedef void(^selectBlock)(BOOL isSelected);
@interface ShoppingCartTableViewCell : UITableViewCell
@property (nonatomic, copy) selectBlock selectedBlock;
@property (nonatomic, strong) GoodsModel *model;
@property (nonatomic, strong) UIButton *selectedIconButton;
@property (nonatomic, strong) UIImageView *goodsImageView;
@property (nonatomic, strong) UILabel *goodsTitleLabel;
@property (nonatomic, strong) UILabel *goodsContentLabel;
@property (nonatomic, strong) UILabel *totalPriceLabel;
@property (nonatomic, strong) SelectButtonView *selectButton;
+ (instancetype)cellWithTableView:(UITableView *)tableView identifier:(NSString *)identifier rowheight:(CGFloat)height;
@end
.m
#import "ShoppingCartTableViewCell.h"
@implementation ShoppingCartTableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView identifier:(NSString *)identifier rowheight:(CGFloat)height {
ShoppingCartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[ShoppingCartTableViewCell alloc] initWithIdentifier:identifier rowheight:height];
}
return cell;
}
- (instancetype)initWithIdentifier:(NSString *)identifier rowheight:(CGFloat)height {
self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.selectedIconButton = [[UIButton alloc] initWithFrame:CGRectMake(30*ScaleSize, 0, 40*ScaleSize, 40*ScaleSize)];
self.selectedIconButton.centerY = height/2.0f;
[self.selectedIconButton setImage:[UIImage imageNamed:@"button_unchoice"] forState:UIControlStateNormal];
[self.selectedIconButton setImage:[UIImage imageNamed:@"button_choice"] forState:UIControlStateSelected];
[self.selectedIconButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.selectedIconButton];
self.goodsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100*ScaleSize, 0, 190*ScaleSize, 190*ScaleSize)];
[self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:@""]];
self.goodsImageView.layer.cornerRadius = 5;
self.goodsImageView.layer.masksToBounds = YES;
self.goodsImageView.centerY = height/2.0f;
[self.goodsImageView setBorderWithWidth:0.5 color:UIColorFromRGB(0xf1f1f1)];
[self addSubview:self.goodsImageView];
self.goodsTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(305*ScaleSize, 10*ScaleSize, ScreenWidth - 315*ScaleSize, 75*ScaleSize)];
self.goodsTitleLabel.text = @"";
self.goodsTitleLabel.numberOfLines = 2;
self.goodsTitleLabel.textColor = UIColorFromRGB(0x3a3a3a);
self.goodsTitleLabel.font = [UIFont defaultFontOfSize:15];
[self addSubview:self.goodsTitleLabel];
self.goodsContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsTitleLabel.left, self.goodsTitleLabel.bottom + 10*ScaleSize, self.goodsTitleLabel.width, 30*ScaleSize)];
self.goodsContentLabel.text = @"";
self.goodsContentLabel.textColor = UIColorFromRGB(0x949494);
self.goodsContentLabel.font = [UIFont defaultFontOfSize:14];
[self addSubview:self.goodsContentLabel];
self.totalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.goodsTitleLabel.left, 0, self.goodsTitleLabel.width - 200*ScaleSize, 34*ScaleSize)];
self.totalPriceLabel.text = @"¥0.00";
self.totalPriceLabel.bottom = self.goodsImageView.bottom - 10*ScaleSize;
self.totalPriceLabel.textColor = UIColorFromRGB(0xff5438);
self.totalPriceLabel.font = [UIFont defaultFontOfSize:16];
[self addSubview:self.totalPriceLabel];
self.selectButton = [[SelectButtonView alloc] initWithFrame:CGRectMake(0, 0, 170*ScaleSize, 45*ScaleSize)];
self.selectButton.right = ScreenWidth - 30*ScaleSize;
self.selectButton.centerY = self.totalPriceLabel.centerY;
[self addSubview:self.selectButton];
}
return self;
}
- (void)setModel:(GoodsModel *)model {
[self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.goodsHeaderImage]];
if (model.isSelected == YES) {
self.selectedIconButton.selected = YES;
} else {
self.selectedIconButton.selected = NO;
}
self.goodsTitleLabel.text = model.goodsName;
self.goodsContentLabel.text = model.selectedSize;
self.totalPriceLabel.text = [NSString stringWithFormat:@"¥%@", model.goodsPrice];
[self.selectButton setItemNum:[model.number integerValue]];
}
- (void)buttonAction:(UIButton *)button {
button.selected = !button.selected;
if (button.selected) {
self.selectedBlock(YES);
} else {
self.selectedBlock(NO);
}
}
这样一来,只需要在cell赋值的时候把block丢进来。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ShoppingCartTableViewCell *cell = [ShoppingCartTableViewCell cellWithTableView:tableView identifier:@"shop" rowheight:220*ScaleSize];
__weak typeof(self) weakSelf = self;
GoodsModel *model = self.dataArray[indexPath.row];
cell.selectButton.block = ^(NSString*str) {
model.number = str;
[weakSelf getCountPrice];
};
cell.selectedBlock = ^(BOOL isSelected) {
model.isSelected = isSelected;
[weakSelf getCountPrice];
};
cell.model = self.dataArray[indexPath.row];
return cell;
}
然后每次点击了之后,都把总价算一算
- (void)getCountPrice {
NSInteger flag = 0;
CGFloat count = 0;
NSInteger number = 0;
for (GoodsModel *temp in self.dataArray) {
if (temp.isSelected != YES) {
flag ++;
} else {
number += [temp.number intValue];
count += ([temp.goodsPrice floatValue] * [temp.number intValue]);
}
}
if (flag == 0) {
allPickButton.selected = YES;
} else {
allPickButton.selected = NO;
}
if (number == 0) {
[goPayButton setTitle:@"去结算" forState:UIControlStateNormal];
} else {
[goPayButton setTitle:[NSString stringWithFormat:@"去结算(%ld)", number] forState:UIControlStateNormal];
}
totalPriceLabel.text = [NSString stringWithFormat:@"合计:¥%.2f", count];
}
搞定!