iOS购物车简单实现(无动画)

首先分为三种情况,分别是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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,607评论 1 9
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,107评论 29 470
  • 01 就像是生锈了一样。 待在潮湿的地方太久了。 事实上我很喜欢雨天。 02 每个人都有每个人想要去做的事情。 有...
    沈夏仪阅读 202评论 0 3
  • BUPT校内网使用网页登录的方式认证,不需要安装额外的客户端,从而避免了各种意义上的适配问题。但是这个方法也有一个...
    橙子是气球杀手阅读 650评论 0 0
  • 课程概要:1、非线性方程组2、数值积分3、常微分方程组 1、非线性方程组 2、数值积分(定积分) 3、常微分方程组
    LuCh1Monster阅读 6,584评论 0 7