iOS仿美团购物车

个人博客: http://blog.sina.com.cn/fengyingjie888

iOS仿美团购物车_第1张图片
ddd.gif

这里就以我们项目的特价商品为例吧! 先来写加的情况,看看需要什么数据 就储存什么数据
//这里是为取数据作准备 selectedArr是存储数据的数组

SpecialPriceModel *mod = dataFourArr[btn.tag];
if (selectedArr.count > 0) 
{

//isHave 是为了确定数组里面到底有没有该商品 
__block int isHave = 0; 
 [selectedArr enumerateObjectsUsingBlock:id _Nonnull obj,       NSUInteger idx,        BOOL * _Nonnull stop { 
//这是数组里面如果有就把数量num + 1 再存进去
 if ([obj[@"productid"] isEqualToString:mod.productid])     
 {


  NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
          int num = [obj[@"num"] intValue] + 1;
         [dataDic setObject:[NSString stringWithFormat:@"%ld",btn.tag] forKey:@"idex"];
         [dataDic setObject:mod.marketprice forKey:@"marketprice"];
         [dataDic setObject:mod.productid forKey:@"productid"];
         [dataDic setObject:mod.productname forKey:@"productname"];
         [dataDic setObject:[NSString stringWithFormat:@"%d",num] forKey:@"num"];
         [selectedArr removeObject:obj];
         [selectedArr addObject:dataDic];
         isHave = 1;

    //数组升序排列 为了避免BUG
             NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"idex" ascending:YES];
         NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:&sortDes1 count:1];
         [selectedArr sortUsingDescriptors:sortDescriptors];
     }

 }];```

isHave == 0 说明数组中还没有添加该商品 把需要的参数传进去 num置为1就好了

if (isHave == 0) 
{ 
NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
 [dataDic setObject:[NSString stringWithFormat:@"%ld",btn.tag]   forKey:@"idex"]; 
[dataDic setObject:mod.marketprice forKey:@"marketprice"]; 
[dataDic setObject:mod.productid forKey:@"productid"]; 
[dataDic setObject:mod.productname forKey:@"productname"]; 
[dataDic setObject:@"1" forKey:@"num"];
 [selectedArr addObject:dataDic]; 
//数组升序排列 
NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc]   initWithKey:@"idex" ascending:YES];
 NSMutableArray *sortDescriptors = [[NSMutableArray alloc]     initWithObjects:&sortDes1 count:1];
 [selectedArr sortUsingDescriptors:sortDescriptors]; 
}


}

这是一个商品还没选得时候,我把它单独列为一个情况

if (selectedArr.count == 0) 
{
 NSMutableDictionary *dataDic = [NSMutableDictionary dictionary]; 
[dataDic setObject:[NSString stringWithFormat:@"%ld",btn.tag] forKey:@"idex"];
 [dataDic setObject:mod.marketprice forKey:@"marketprice"]; 
[dataDic setObject:mod.productid forKey:@"productid"]; 
[dataDic setObject:mod.productname forKey:@"productname"]; 
[dataDic setObject:@"1" forKey:@"num"]; 
[selectedArr addObject:dataDic]; }

下面是减数量的情况 (就是点减号的时间)

SpecialPriceModel *mod = dataFourArr[btn.tag]; 
[selectedArr enumerateObjectsUsingBlock:id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop 
{ 
//找到该商品 然后判断他得num 如果大于1 就 -1 再存进去 如果=1 说明    num要-为0 所以直接删除就好了 
if ([obj[@"productid"] isEqualToString:mod.productid]) 
{ 
 if ([obj[@"num"] intValue] > 1) 
{
NSMutableDictionary *dataDic = [NSMutableDictionary dictionary]; 
int num = [obj[@"num"] intValue] - 1; 
[dataDic setObject:[NSString stringWithFormat:@"%ld",btn.tag] forKey:@"idex"]; 
[dataDic setObject:mod.marketprice forKey:@"marketprice"]; 
[dataDic setObject:mod.productid forKey:@"productid"]; 
[dataDic setObject:mod.productname forKey:@"productname"]; 
  [dataDic setObject:[NSString stringWithFormat:@"%d",num] forKey:@"num"]; 
 [selectedArr removeObject:obj]; 
 [selectedArr addObject:dataDic]; 
 //数组升序排列 
    NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc]   initWithKey:@"idex" ascending:YES]; 
NSMutableArray *sortDescriptors = [[NSMutableArray alloc]     initWithObjects:&sortDes1 count:1];
 [selectedArr sortUsingDescriptors:sortDescriptors]; 
 } 
if ([obj[@"num"] intValue] == 1) 
 { 
[selectedArr removeObject:obj];
 }
     }

 }];

下面来说 在表中得操作

 SpecialPriceModel *mod = dataFourArr[indexPath.row];

isHave是来确定购物车中是否有该商品 如果有 就把 减号和数量显示出来 
  __block int isHave = 0; 
  [selectedArr enumerateObjectsUsingBlock:id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop { 
if ([obj[@"productid"] isEqualToString:mod.productid]) 
 { 
 [cell.jianBtn setHidden:NO];
  [cell.payNumLab setHidden:NO];
 cell.payNumLab.text = obj[@"num"];
isHave = 1;
 }
    }];

如果没有就直接隐藏掉

if (isHave == 0)
 { 
 [cell.jianBtn setHidden:YES];  [cell.payNumLab setHidden:YES];

    }

你可能感兴趣的:(iOS仿美团购物车)