简单的购物车功能实现

实现一个简单的购物车的功能,界面比较丑见笑了,主要是说一下实现的思路,写起来还是挺简单的。


简单的购物车功能实现_第1张图片
购物车Demo.jpeg

大致的实现思路是:
1.model负责数据的存取
2.自定义的cell负责展示model中的数据
3.通过cell上的按钮点击事件获取到cell对应的model,然后更改数据再刷新界面


定义model类

首先定义一个model,用来存放cell需要显示的数据,这里为了简单只定义了三个必须的属性,具体的名称、图片等还需要根据需求来定

@interface PXYCargoModel : NSObject
//商品数量
@property (nonatomic, assign) int count;
//是否选中
@property (nonatomic, assign) BOOL isSelected;
//商品价格
@property (nonatomic, assign) float price;

@end

自定义cell

这个就按照需求去自定义就好了,主要是三个按钮和一个记录选中状态的Bool值

//减按钮
@property (nonatomic, strong) UIButton *minusButton;
//加按钮
@property (nonatomic, strong) UIButton *addButton;
//选中按钮
@property (nonatomic, strong) UIButton *selectButton;
//记录选中状态
@property (nonatomic, assign) BOOL isSelected;

封装一下用model给cell的属性赋值的方法:
- (void)configCellWithModel:(PXYCargoModel *)model{
//购买数量
self.countLabel.text = [NSString stringWithFormat:@"%d",model.count];
//商品价格
self.priceLabel.text = [NSString stringWithFormat:@"%.2f",model.price];
}
设置一下三个按钮的tag值,分别设定为100001、100002、100003。实现一个统一的响应事件,点击“选中按钮”时需要切换一下按钮图片:
- (void)buttonAction:(UIButton *)sender{
//点击选中按钮时需要切换图片
if (sender.tag == 100003) {
self.isSelected = !self.isSelected;
if (self.isSelected) {
[self.selectButton setImage:[UIImage imageNamed:@"selected_btn"] forState:UIControlStateNormal];
}else{
[self.selectButton setImage:[UIImage imageNamed:@"selected_btn2"] forState:UIControlStateNormal];
}
}
//调用代理方法
if (self.delegate && [self.delegate respondsToSelector:@selector(tableViewCell:didButtonClickedWithTag:)]) {
[self.delegate tableViewCell:self didButtonClickedWithTag:sender.tag];
}
}
这里边调用了一个自定义的一个代理方法,需要的参数为cell本身以及对应的按钮的tag值,这是当点击cell上的按钮时能获取到对应的model值的关键。
这是自定义cell的.h文件:

@class PXYTableViewCell;
@protocol PXYTableViewCellDelegate 

- (void)tableViewCell:(PXYTableViewCell *)cell didButtonClickedWithTag:(NSInteger)tag;

@end

@interface PXYTableViewCell : UITableViewCell

@property (nonatomic, weak)id delegate;

- (void)configCellWithModel:(PXYCargoModel *)model;

@end

定义ViewController

接下来定义一个控制器,用来展示cell,实现对应的逻辑。

    @interface PXYTableViewController ()    
    //展示数据的tableView
    @property (nonatomic, strong) UITableView *tableView;
    //数据源数组
    @property (nonatomic, strong) NSMutableArray *dataArray;
    //页面底部总价Label
    @property (nonatomic, strong) UILabel *totalPriceLabel;
    //去结算按钮
    @property (nonatomic, strong) UIButton *jiesuanButton;
    @end

在ViewDidLoad里配置一下UI,制造几个model数据用于模拟 (实际的话model数据是由网络请求或读取本地存储而来) 。然后实现tableView的代理方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PXYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
PXYCargoModel *model = self.dataArray[indexPath.row];
[cell configCellWithModel:model];
return cell;
}
然后实现cell的代理方法,通过传入的cell来获取数组中对应的model对象,通过点击按钮来改变model中对应的值,然后根据是否被选中以及对应的个数、单价计算出总价:
- (void)tableViewCell:(PXYTableViewCell *)cell didButtonClickedWithTag:(NSInteger)tag{
//通过传入的cell来获取indexPath值
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//通过indexPath值获取到对应的model对象
PXYCargoModel *model = self.dataArray[indexPath.row];
switch (tag) {
//减号按钮点击,不能显示负数
case 100001:
model.count - 1 >= 0 ? model.count -= 1 : 0;
break;
//加号按钮点击
case 100002:
model.count += 1;
break;
//选中按钮点击
case 100003:
model.isSelected = !model.isSelected;
break;
default:
break;
}
//只有被选中的商品才算入总价
float total = 0.0;
for (PXYCargoModel *model in self.dataArray) {
if (model.isSelected) {
total = total + model.price * model.count;
}
}
self.totalPriceLabel.text = [NSString stringWithFormat:@"%.2f",total];
[self.tableView reloadData];

}

ok,一个基本的购物车的功能就实现了,关键点就是通过自定义的cell的代理方法,传入cell来获取到cell对应的数据。然后就是一个简单的MVC思想,cell负责显示model中的数据,点击按钮改变的是数据源,然后刷新界面。

你可能感兴趣的:(简单的购物车功能实现)