iOS 购物车的实现

                              iOS 购物车的实现

   思路: 通过model中绑定一个boll值来判断每一行Cell中的选中按钮, 定义可变数组来保存选中的数据,可方便的传到结算页面. 需要源码的朋友可以留下邮箱啊, 主要代码如下:

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,tableFootViewDelegate>

@property (weak, nonatomic) IBOutletUITableView *tableView;

@property (nonatomic,strong)TableHaerdView *headerView;

@property (nonatomic,strong)TableFootView *footView;

//存放数据数组

@property (nonatomic,strong)NSMutableArray *dataArray;

//存放数据字典`

@property (nonatomic,strong)NSMutableDictionary *mutableDictionary;

@property (nonatomic,assign)float tempTotalPirce;

@property (nonatomic,strong)NSMutableArray *payArray;

@property (nonatomic,strong)NSMutableArray *isSelectArray;

@end

//cell标识

staticNSString *indentifier = @"cell";



@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

   //使用storboard不需要注册cell

    [self.tableViewregisterClass:[GoShoppingCellclass] forCellReuseIdentifier:indentifier];

    

    

   self.title =@"购物车";

    //数据源

    [selfsetUpModel];

    //headerView

    [selfsetUpHeaderView];

    //footView

    [selfsetupFootView];

    

    

}

- (void)viewWillAppear:(BOOL)animated{

    [superviewWillAppear:animated];


   for (int i =0; i< self.dataArray.count; i++) {

       GoShoppingModel *model = self.dataArray[i];

        model.isSelect =NO;

        

    }

    

    

    [_tableView reloadData];

}


#pragma mark -- footView

- (void)setupFootView {

   _footView = [[TableFootViewalloc]init];

    _footView.delegate =self;

//    _footView.alpha = 0;

    [self.viewaddSubview:_footView];

    [_footViewmas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.trailing.leading.equalTo(self.view);

        make.height.equalTo(@60);

        make.bottom.equalTo(self.view.mas_bottom).offset(0);

    }];


}


#pragma mark -- headerView

- (void)setUpHeaderView{

    

   CGRect statusFrame = [[UIApplicationsharedApplication]statusBarFrame];

   CGRect navFrame =self.navigationController.navigationBar.frame;

    

   TableHaerdView *headerView = [[TableHaerdViewalloc]initWithFrame:CGRectMake(0, navFrame.size.height+statusFrame.size.height,self.view.frame.size.width,50)];

   _headerView = headerView;

    //点击全选回调

    headerView.slelectBlock =^(NSInteger integer){

        //定义临时变量保存价格;

       float totalPirce = 0.0;

           for (int i =0; i<self.dataArray.count; i++) {

                

               GoShoppingModel *model = self.dataArray[i];

               //设置选中状态

                model.isSelect = integer;

               if (model.isSelect) {

                    

                   self.footView.alpha =1.0;

                   //记录总价格

                    totalPirce += [model.goodsPriceintValue];

                   //显示价格

                   self.footView.totalPirce = totalPirce;

                    //把价格保存到变量里面给后期点击每一个btn使用

                   self.tempTotalPirce = totalPirce;

                    [self.payArrayaddObject:model];


                }else {

                    

//                    self.footView.alpha = 0.0;

                    totalPirce =0.0;

                   self.tempTotalPirce = totalPirce;

                   //显示价格

                   self.footView.totalPirce = totalPirce;

                   //移除数组防止数据重复

                    [self.payArrayremoveAllObjects];

                    

            }

                

                [self.tableViewreloadData];

                

            }

        //如果数组没有数据

       if (!self.dataArray.count){

           // 提示窗

            [selfalertActionControllerWithMessage:@"购物车空空如也!快去挑选宝贝吧!"];


           self.headerView.selectBtn.selected =NO;

        }        

        //每次价格都要清除

        totalPirce =0.0;


    };

    

    [self.viewaddSubview:headerView];

//    //设置顶部多余距离

    self.automaticallyAdjustsScrollViewInsets=NO;

}




#pragma mark--- footView代理


- (void)tableFootViewWithTableFootView:(TableFootView *)footView{


    if (self.tempTotalPirce) {

        

        PayViewController *payVC = [[PayViewControlleralloc]init];

        payVC.totalPirce =self.tempTotalPirce;

        payVC.payArray =self.payArray.copy;

        

       //移除数组

       if (self.dataArray.count ==self.payArray.count) {

            [self.dataArrayremoveAllObjects];

        }else{

           for (int i =0; i<self.payArray.count; i++) {

                

               GoShoppingModel *model = self.payArray[i];

                [self.dataArrayremoveObject:model];

            }

        

        }

        

        //调到控制器清空数组和总价格

        [self.payArrayremoveAllObjects];

        self.tempTotalPirce =0.0;

       //总价格

        self.footView.totalPirce =self.tempTotalPirce;

       //全选按钮

        self.headerView.isInteger =NO;



        [self.navigationControllerpushViewController:payVC animated:NO];

        


       NSLog(@"%lu",(unsignedlong)self.payArray.count);

        

    }else{

        //如果没有选择商品提示弹窗

        

        [selfalertActionControllerWithMessage:@"还没有选择宝贝"];

        

    }

  

}


#pragma mark --提示窗


- (void)alertActionControllerWithMessage:(NSString*)message{


    UIAlertController *alertVc = [UIAlertControlleralertControllerWithTitle:@"提示"message:message preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *action = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        

    }];

    

    //[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];

    

    [alertVcaddAction:action];

    

    [selfpresentViewController:alertVc animated:YEScompletion:nil];




}



#pragma mark -- TableView代理方法


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


      return self.dataArray.count;

}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    GoShoppingCell *cell = [tableViewdequeueReusableCellWithIdentifier:indentifierforIndexPath:indexPath];

    

    //cell赋值

   GoShoppingModel *model = self.dataArray[indexPath.row];

    model.tag =@(indexPath.row);

    cell.model = model;

    

    //点击btn回调

    [selfbtnClickWithCell:cell];

    

  

   return cell;

}


#pragma mark -- 点击but逻辑处理


- (void)btnClickWithCell:(GoShoppingCell*)cell{


    //点击btn回调

    cell.selectBlock = ^(UIButton *btn){

        

       self.footView.alpha =1.0;

       GoShoppingModel *model = self.dataArray[btn.tag];

        

       if (!btn.selected) {

            

           _tempTotalPirce -= [model.goodsPriceintegerValue];

            self.footView.totalPirce =_tempTotalPirce;

            model.isSelect =NO;

           for (int i =0; i<self.payArray.count; i++) {

               GoShoppingModel *payModel = self.payArray[i];

                

               if ([payModel.goodsNameisEqual:model.goodsName]) {

                    

                    [self.payArrayremoveObjectAtIndex:i];

                }

                

            }

            

            

        }else{

            

           _tempTotalPirce += [model.goodsPriceintegerValue];

            self.footView.totalPirce =_tempTotalPirce;

            

            [self.payArrayaddObject:model];

             model.isSelect =YES;

            

            

           if (self.payArray.count ==self.dataArray.count) {

                

               self.headerView.isInteger =YES;

            }

            

        }

        

    };

    


}



#pragma mark - 初始化数据


- (void)setUpModel{


   for (int i =0; i<10; i++) {

        self.mutableDictionary = [[NSMutableDictionaryalloc]init];

        

        [self.mutableDictionary  setValue:[NSStringstringWithFormat:@"iphone%d",i]forKey:@"goodsName"];

        [self.mutableDictionarysetValue:[NSStringstringWithFormat:@"%d",i]forKey:@"goodsPrice"];

        [self.mutableDictionarysetValue:[NSNumbernumberWithBool:NO]forKey:@"isSelect"];

        GoShoppingModel *model = [GoShoppingModelGShoppingModelWithDict:self.mutableDictionary];

        [self.dataArrayaddObject:model];

    }

}


#pragma mark== 懒加载数组


- (NSMutableArray *)dataArray{

   if (!_dataArray) {

        

       _dataArray = [NSMutableArrayarray];

    }

    return_dataArray;

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];


}

- (NSMutableArray *)payArray{

   if (!_payArray) {

        

       _payArray = [[NSMutableArrayalloc]init];

    }

    return_payArray;

}



- (NSMutableArray *)isSelectArray{

    if (!_isSelectArray) {

        

       _isSelectArray = [NSMutableArrayarray];

    }

    return_isSelectArray;

}

- (void)dealloc{

    

    [[NSNotificationCenterdefaultCenter ] removeObserver:selfname:@"senderSelect"object:nil];


}


@end



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值