iOS 購物車功能的實(shí)現(xiàn)

003nsTb1zy76fZXUAjt57&690.gif

首先分為三種情況,分別是1.點(diǎn)擊單個(gè)商品 2.點(diǎn)擊商店選中該商店的所有商品 3.全選

分別創(chuàng)建按鈕? 單個(gè)商品的按鈕的tag值為indexPath.row 商店按鈕為 btn.tag = section + 10000; 全選為btn.tag = -10;好了這是我得設(shè)定 三個(gè)按鈕方法為一個(gè)

//先確定購物車有商品

if (shopArr.count == 0)


{

[selfshowAlertWithPoint:2

                    text:@"你的購物車還沒有商品哦!快去添加吧"

                   color:UICYAN];

return;

}

ShoppingCarModel *mod = shopArr[indexPath.section];



carOrderListModel *lMod = mod.orderList[indexPath.row];///為了一會(huì)方便取數(shù)據(jù)

NSNumber *number =[NSNumbernumberWithInteger:indexPath.section];


NSNumber *number1 =[NSNumbernumberWithInteger:indexPath.row];

?//先確定當(dāng)前點(diǎn)擊按鈕有沒有被選中

__blockBOOL isSelected =NO;


__blockBOOL sectionIsSelected =NO;

//數(shù)組升序排列為了解決先存下面的再存上面的 導(dǎo)致根據(jù)索引取值的時(shí)候會(huì)出錯(cuò)(必須多點(diǎn)一次商店的按鈕) 雖然不是大BUG 但是為了追求完美就重新排列下數(shù)組


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


 {

//單行和商店按鈕 全選會(huì)是另一個(gè)值來判斷?


 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]])

         {

             //進(jìn)到此處表示這一行剛才操作過

             isSelected = YES;/// Variable is not assignable (missing __block type specifier)

         }

         }];

 }

 if ([obj[@"a"] isEqualToString:[NSStringstringWithFormat:@"%ld",button.tag - 10000]])

 {

     //這個(gè)區(qū)被選過

     sectionIsSelected = YES;

 }

 }];

//當(dāng)點(diǎn)擊商店按鈕的時(shí)候

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];

    //記錄當(dāng)前單元格的索引

    [button setImage:[UIImageimageNamed:@"sele"] forState:UIControlStateNormal];

// NSLog(@"數(shù)組%@",_selectedArr);


}

[tablereloadData];

}

?

//當(dāng)點(diǎn)擊某個(gè)商品按鈕的時(shí)候

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];

        //數(shù)組字典之類的存的都是對象數(shù)據(jù)類型 >>>如果遇到整數(shù)類型的可以考慮轉(zhuǎn)換成NSNumber

        //記錄當(dāng)前單元格的索引

        [button setImage:[UIImageimageNamed:@"sele"] forState:UIControlStateNormal];

    }



}

}

//當(dāng)點(diǎn)擊全選按鈕的時(shí)候?

? if (button.tag == -10)

{

//遍歷記錄索引的數(shù)組  如果數(shù)組里面的索引和當(dāng)前點(diǎn)擊單元格的indexPath相等

//1.找單元格

if (All)

{

    //刪掉

    [_selectedArrremoveAllObjects];

    [button setImage:[UIImageimageNamed:@"nosele"] forState:UIControlStateNormal];

    All = NO;

}

else

{

    All = YES;



    [_selectedArrremoveAllObjects];

    for (int a = 0; a <</span> 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];

    }

    //記錄當(dāng)前單元格的索引

    [button setImage:[UIImageimageNamed:@"sele"] forState:UIControlStateNormal];

// NSLog(@"數(shù)組%@",_selectedArr);


}

[tablereloadData];

}

?///執(zhí)行完上面以后就可以結(jié)算價(jià)格了

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) {

     //進(jìn)到此處表示剛才操作過

     priceNum = [obj1[@"c"] floatValue] + priceNum;

     count ++;

     [str insertString:[NSStringstringWithFormat:@"%@,",obj1[@"d"]] atIndex:0];

 }];

 }];

oneCardId = [NSStringstringWithFormat:@"%@",str];

// NSLog(@"錢數(shù)還有多少$%f",priceNum);

//因?yàn)槲覀兊馁徫镘囀莾煞N貨幣 所以要判斷狀態(tài)?

if ([typeisEqualToString:@"1"])

{

labPrice.text = [NSStringstringWithFormat:@"你將支付:%.2f元",priceNum];

}

else

{

labPrice.text = [NSStringstringWithFormat:@"你將支付:%.2f龍點(diǎn)幣",priceNum];

}

[payBtnsetTitle:[NSStringstringWithFormat:@"結(jié)算(%lu)",(unsignedlong)count] forState:UIControlStateNormal];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市了袁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌搂赋,老刑警劉巖帖努,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梢薪,死亡現(xiàn)場離奇詭異歉备,居然都是意外死亡逊彭,警方通過查閱死者的電腦和手機(jī)咸灿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來侮叮,“玉大人,你說我怎么就攤上這事悼瘾∧野瘢” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵亥宿,是天一觀的道長卸勺。 經(jīng)常有香客問我,道長烫扼,這世上最難降的妖魔是什么曙求? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮映企,結(jié)果婚禮上悟狱,老公的妹妹穿的比我還像新娘。我一直安慰自己堰氓,他們只是感情好挤渐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著双絮,像睡著了一般浴麻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上囤攀,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天软免,我揣著相機(jī)與錄音,去河邊找鬼焚挠。 笑死膏萧,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播向抢,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼认境,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了挟鸠?” 一聲冷哼從身側(cè)響起叉信,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎艘希,沒想到半個(gè)月后硼身,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡覆享,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年佳遂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片撒顿。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡丑罪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出凤壁,到底是詐尸還是另有隱情吩屹,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布拧抖,位于F島的核電站煤搜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏唧席。R本人自食惡果不足惜擦盾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望淌哟。 院中可真熱鬧迹卢,春花似錦、人聲如沸绞绒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蓬衡。三九已至喻杈,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狰晚,已是汗流浹背筒饰。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留壁晒,地道東北人瓷们。 一個(gè)月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谬晕。 傳聞我的和親對象是個(gè)殘疾皇子碘裕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容