购物车全选、单选相关处理

最近由于换工作,好长时间没发表文章了,趁今晚闲来,发表一下,购物车处理相关的Demo

1.gif

单选、全选处理

//全选
- (IBAction)clikeMainSleocted:(UIButton *)sender {
    
    allprice = 0;
     __weak typeof(self) weakSelf = self;
    
    [self.data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        WineModel *mod = obj;
        mod.seleoctedShop = !sender.selected;

        
        if(mod.seleoctedShop == NO){
            
            self.moneyMain.text = @"0";
        }else{
            
            int  total = mod.money.intValue * mod.shopCount;
            allprice = allprice + total;
            weakSelf.moneyMain.text = [NSString stringWithFormat:@"%d",allprice];
            
        }
        
         
    }];
    
    [self.table reloadData];

    sender.selected = ! sender.selected;
}
//选中
- (void)TableViewCellWithClikeSeleocCell:(TableViewCell *)cell{
    
    if(cell.mod.seleoctedShop){
        
        int total =  self.moneyMain.text.intValue + cell.mod.money.intValue * cell.mod.shopCount;
        self.moneyMain.text = [NSString stringWithFormat:@"%d",total];
        
    }else{
        
        int total =  self.moneyMain.text.intValue -   cell.mod.money.intValue * cell.mod.shopCount;
        self.moneyMain.text = [NSString stringWithFormat:@"%d",total];

    }
    
    
    if(self.shopCar.count == self.data.count){
        
        self.seleocMain.selected = YES;
    }else{
        self.seleocMain.selected = NO;
    }
    
}

加、减处理

//加
- (void)TableViewCellWithClikeAddCell:(TableViewCell *)cell{
    
    int main = self.moneyMain.text.intValue + cell.mod.money.intValue;
    self.moneyMain.text = [NSString stringWithFormat:@"%d",main];
    if(![self.shopCar containsObject:cell.mod]){
        
        [self.shopCar addObject:cell.mod];
    }
    
}
//减
- (void)TableViewCellWithClikeDeleocCell:(TableViewCell *)cell{
    
    int main = self.moneyMain.text.intValue - cell.mod.money.intValue;
    self.moneyMain.text = [NSString stringWithFormat:@"%d",main];
    if(cell.mod.shopCount == 0){
        
        [self.shopCar removeObject:cell.mod];
    }
}

购物车处理Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • ********《MVC模式下的购物车》******** ViewController: 购物车界面 整个界面就是...
    带你去旅行阅读 4,659评论 4 21
  • 转载链接 注:本文转载知乎上的回答 作者:初雪 链接:https://www.zhihu.com/question...
    pengshuangta阅读 28,805评论 9 295
  • 孤独的夜 唯有时光悄悄流泻 而流泻的时光里 总有些年少的心在风中摇曳 摇曳 然后毁灭 之后 岁月更迭 是否有人 为...
    须臾331阅读 176评论 0 1
  • 8.8 工作充实 看了一集快乐大本营很快乐 窦靖童
    来莹莹阅读 271评论 0 0