首先分为三种情况,分别是1.点击单个商品 2.点击商店选中该商店的所有商品 3.全选
分别创建按钮 单个商品的按钮的tag值为indexPath.row 商店按钮为 btn.tag = section + 10000; 全选为btn.tag = -10
//确定购物车有商品
if (shopArr.count == 0)
{
[selfshowAlertWithPoint:2
text:@"无商品"
color:ZLCYAN];
return;
}
ShoppingCarModel *mod = shopArr[indexPath.section];
carOrderListModel *lMod = mod.orderList[indexPath.row];///方便取数据
NSNumber *number =[NSNumbernumberWithInteger:indexPath.section];
NSNumber *number1 =[NSNumbernumberWithInteger:indexPath.row];
//确定当前点击按钮有没有被选中
__blockBOOL isSelected =NO;
__blockBOOL sectionIsSelected =NO;
//数组升序排列为了解决先存下面的再存上面的 导致根据索引取值的时候会出错
NSSortDescriptor *sortDes1 = [[NSSortDescriptor alloc] initWithKey:@"a"ascending:YES];
NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:&sortDes1 count:1];
[_selectedArr sortUsingDescriptors:sortDescriptors];
[_selectedArrenumerateObjectsUsingBlock:id _Nonnull obj, NSUInteger idx1, BOOL * _Nonnull stop
{
//单行和商店按钮 全选会是另一个值来判断
if ([obj[@"a"] isEqualToString:[NSStringstringWithFormat:@"%@",number]])
{
NSArray *arr = obj[@"row"];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj1[@"b"] isEqualToString:[NSStringstringWithFormat:@"%@",number1]])
{
//进到此处表示这一行刚才操作过
isSelected = YES;/// Variable is not assignable (missing __block type specifier)
}
}];
}
if ([obj[@"a"] isEqualToString:[NSStringstringWithFormat:@"%ld",button.tag - 10000]])
{
//这个区被选过
sectionIsSelected = YES;
}
}];
//当点击商店按钮的时候
if (button.tag > 9999)
{
//1.找单元格
if (sectionIsSelected)
{
[_selectedArrenumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
if ([obj[@"a"] isEqualToString:[NSStringstringWithFormat:@"%ld",button.tag - 10000]]) {
//删掉
[_selectedArrremoveObject:obj];
if (button.tag != - 10)
{
All = NO;
}
[button setImage:[UIImageimageNamed:@"nosele"] forState:UIControlStateNormal];
}
}];
}
if (!sectionIsSelected)
{
NSMutableDictionary *sectionDic = [NSMutableDictionarydictionary];
NSMutableArray *rowArr = [NSMutableArrayarray];
ShoppingCarModel *mod4 = shopArr[button.tag - 10000];
[_selectedArrenumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
{
if ([obj[@"a"] isEqualToString:[NSStringstringWithFormat:@"%ld",button.tag - 10000]])
{
[_selectedArrremoveObject:obj];
}
}];
for (int b = 0; b < mod4.orderList.count; b ++)
{
NSMutableDictionary *rowDic = [NSMutableDictionarydictionary];
carOrderListModel *mod5 = mod4.orderList[b];
NSString *price = [NSStringstringWithFormat:@"%f",[mod5.marketPricefloatValue] * [mod5.numfloatValue]];
[rowDic setObject:[NSStringstringWithFormat:@"%d",b] forKey:@"b"];
[rowDic setObject:price forKey:@"c"];
[rowDic setObject:mod5.cartIdforKey:@"d"];
[rowArr addObject:rowDic];
}
[sectionDic setObject:[NSStringstringWithFormat:@"%ld",(long)button.tag - 10000] forKey:@"a"];
[sectionDic setObject:rowArr forKey:@"row"];
[_selectedArraddObject:sectionDic];
//记录当前单元格的索引
[button setImage:[UIImageimageNamed:@"sele"] forState:UIControlStateNormal];
// NSLog(@"数组%@",_selectedArr);
}
[tablereloadData];
}
//当点击某个商品按钮的时候
if (button.tag > -1 && button.tag < 9999)
{
//1.找单元格
if (isSelected)
{
NSNumber *number =[NSNumbernumberWithInteger:indexPath.section];
NSNumber *number1 =[NSNumbernumberWithInteger:indexPath.row];
[_selectedArrenumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx1, BOOL * _Nonnull stop)
{
if ([obj[@"a"] isEqualToString:[NSStringstringWithFormat:@"%@",number]])
{
NSMutableArray *arr = obj[@"row"];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj1[@"b"] isEqualToString:[NSStringstringWithFormat:@"%@",number1]])
{
//删掉
[arr removeObject:obj1];
if (button.tag != - 10)
{
All = NO;
}
}
}];
}
}];
[button setImage:[UIImageimageNamed:@"nosele"] forState:UIControlStateNormal];
[seleBtnsetImage:[UIImageimageNamed:@"nosele"] forState:UIControlStateNormal];
}
if (!isSelected)
{
if (button.tag != -10)
{
NSString *num = [NSStringstringWithFormat:@"%f",[lMod.marketPricefloatValue] * [lMod.numfloatValue]];
NSNumber *number =[NSNumbernumberWithInteger:indexPath.section];
NSNumber *numberRow =[NSNumbernumberWithInteger:indexPath.row];
NSMutableArray *rowArr = [NSMutableArrayarray];
NSMutableDictionary *sectionDic = [NSMutableDictionarydictionary];
NSMutableDictionary *rowDic = [NSMutableDictionarydictionary];
[sectionDic setObject:[NSStringstringWithFormat:@"%@",number] forKey:@"a"];
[rowDic setObject:[NSStringstringWithFormat:@"%@",numberRow] forKey:@"b"];
[rowDic setObject:num forKey:@"c"];
[rowDic setObject:lMod.cartIdforKey:@"d"];
[rowArr addObject:rowDic];
[sectionDic setObject:rowArr forKey:@"row"];
// NSLog(@"字典%@",sectionDic);
[_selectedArraddObject:sectionDic];
//数组字典之类的存的都是对象数据类型 >>>如果遇到整数类型的可以考虑转换成NSNumber
//记录当前单元格的索引
[button setImage:[UIImageimageNamed:@"sele"] forState:UIControlStateNormal];
}
}
}
//当点击全选按钮的时候
if (button.tag == -10)
{
//遍历记录索引的数组 如果数组里面的索引和当前点击单元格的indexPath相等
//1.找单元格
if (All)
{
//删掉
[_selectedArrremoveAllObjects];
[button setImage:[UIImageimageNamed:@"nosele"] forState:UIControlStateNormal];
All = NO;
}
else
{
All = YES;
[_selectedArrremoveAllObjects];
for (int a = 0; a < shopArr.count; a ++)
{
NSMutableDictionary *sectionDic = [NSMutableDictionarydictionary];
NSMutableArray *rowArr = [NSMutableArrayarray];
ShoppingCarModel *mod4 = shopArr[a];
for (int b = 0; b < mod4.orderList.count; b ++)
{
NSMutableDictionary *rowDic = [NSMutableDictionarydictionary];
carOrderListModel *mod5 = mod4.orderList[b];
NSString *price = [NSStringstringWithFormat:@"%f",[mod5.marketPricefloatValue] * [mod5.numfloatValue]];
[rowDic setObject:[NSStringstringWithFormat:@"%d",b] forKey:@"b"];
[rowDic setObject:price forKey:@"c"];
[rowDic setObject:mod5.cartIdforKey:@"d"];
[rowArr addObject:rowDic];
}
[sectionDic setObject:[NSStringstringWithFormat:@"%d",a] forKey:@"a"];
[sectionDic setObject:rowArr forKey:@"row"];
[_selectedArraddObject:sectionDic];
}
//记录当前单元格的索引
[button setImage:[UIImageimageNamed:@"sele"] forState:UIControlStateNormal];
// NSLog(@"数组%@",_selectedArr);
}
[tablereloadData];
}
///执行完上面以后就可以结算价格了
priceNum = 0;
count = 0;
NSMutableString *str = [NSMutableStringstring];
[_selectedArrenumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx1, BOOL * _Nonnull stop)
{
NSArray *arr = obj[@"row"];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
//进到此处表示刚才操作过
priceNum = [obj1[@"c"] floatValue] + priceNum;
count ++;
[str insertString:[NSStringstringWithFormat:@"%@,",obj1[@"d"]] atIndex:0];
}];
}];
oneCardId = [NSStringstringWithFormat:@"%@",str];
// NSLog(@"钱数还有多少$%f",priceNum);
labPrice.text = [NSStringstringWithFormat:@"你将支付:%.2f元",priceNum];
[payBtnsetTitle:[NSStringstringWithFormat:@"结算(%lu)",(unsignedlong)count] forState:UIControlStateNormal];