iOS 購物車的實現

                              iOS 購物車的實現數組

   思路: 經過model中綁定一個boll值來判斷每一行Cell中的選中按鈕, 定義可變數組來保存選中的數據,可方便的傳到結算頁面. 須要源碼的朋友能夠留下郵箱啊, 主要代碼以下:iphone

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

@property (weak, nonatomic) IBOutletUITableView *tableView;spa

@property (nonatomic,strong)TableHaerdView *headerView;3d

@property (nonatomic,strong)TableFootView *footView;代理

//存放數據數組orm

@property (nonatomic,strong)NSMutableArray *dataArray;server

//存放數據字典`blog

@property (nonatomic,strong)NSMutableDictionary *mutableDictionary;ip

@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