購物車單選全選價格計算數量增刪等等操作…RAC皆統統搞定.就是這麼cool~
開始之前你需要了解的
配置CocoaPods
安裝CocoaPods命令
1 2 |
gem install cocoapods ##使用RVM安裝的Ruby不需要sudo |
配置ReactiveCocoa
然後在你的Podfile新增一下程式碼
1 2 3 4 5 6 7 |
platform :ios, '8.0' use_frameworks! target '你的專案工程名' do pod 'ReactiveCocoa' end |
最後輸入命令安裝
1 2 |
pod install |
另外常用的pod 命令
1 2 3 |
pod install --verbose --no-repo-update ##安裝不更新的 pod update --verbose --no-repo-update ##更新需要更新的 |
開啟 你的專案工程名.xcworkspace 即可~
RAC在此我就不仔細介紹了,先推薦幾遍文章:
Mattt Thompson寫的ReactiveCocoa
Ash Furrow寫的 Getting Started with ReactiveCocoa
瞭解MVVM
Google了看幾篇有關的文章
MVVM 介紹 譯 朱巨集旭
簡單的介紹一下:
M:model放一些資料模型
V:view檢視
V:viewcontroller控制器
VM:viewmodel主要做處理邏輯和處理資料
開始著手程式碼
專案演示
專案搭建框架
整體檔案目錄按照模組分一下子檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ViewController : ViewController ##載入檢視事件監聽等等 Model : model ##基本資料模型 View : cell ##cell numbercount ##封裝加減控制元件 header ##店鋪之類 footer ##小結 cartbar ##封裝購物車底部view ViewModel : service ##抽離tableview的datasource和delegate viewmodel ##處理主要的邏輯和資料 |
viewmodel類方法屬性解析
獲取資料方法
1.迴圈20個從0到5之間隨機取陣列裡取值加到最終的cartData陣列裡
2.店鋪選擇shopSelectArray預設NO狀態
3.統計總共購物車數量cartGoodsCount
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
- (void)getData{ //資料個數 NSInteger allCount = 20; NSInteger allGoodsCount = 0; NSMutableArray *storeArray = [NSMutableArray arrayWithCapacity:allCount]; NSMutableArray *shopSelectAarry = [NSMutableArray arrayWithCapacity:allCount]; //創造店鋪資料 for (int i = 0; i<allCount; i++) { //創造店鋪下商品資料 NSInteger goodsCount = [_shopGoodsCount[self.random] intValue]; NSMutableArray *goodsArray = [NSMutableArray arrayWithCapacity:goodsCount]; for (int x = 0; x<goodsCount; x++) { JSCartModel *cartModel = [[JSCartModel alloc] init]; cartModel.p_id = @"122115465400"; cartModel.p_price = [_goodsPriceArray[self.random] floatValue]; cartModel.p_name = [NSString stringWithFormat:@"%@這是一個很長很長的名字呀呀呀呀呀呀",@(x)]; cartModel.p_stock = 22; cartModel.p_imageUrl = _goodsPicArray[self.random]; cartModel.p_quantity = [_goodsQuantityArray[self.random] integerValue]; [goodsArray addObject:cartModel]; allGoodsCount++; } [storeArray addObject:goodsArray]; [shopSelectAarry addObject:@(NO)]; } self.cartData = storeArray; self.shopSelectArray = shopSelectAarry; self.cartGoodsCount = allGoodsCount; } |
獲取當前選中價格總和方法
對cartData陣列轉訊號流然後map自定義需求return,期間自定義需求引數value 再次轉訊號流經過filter篩選未選中使isSelectAll為NO,然後經過map自定義需求使model.p_quantity*model.p_price得到的商品總價返回,最終return得到商品總價陣列pricesArray.
快速遍歷pricesArray得出總價allPrices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
- (float)getAllPrices{ __block float allPrices = 0; NSInteger shopCount = self.cartData.count; NSInteger shopSelectCount = self.shopSelectArray.count; if (shopSelectCount == shopCount && shopCount!=0) { self.isSelectAll = YES; } NSArray *pricesArray = [[[self.cartData rac_sequence] map:^id(NSMutableArray *value) { return [[[value rac_sequence] filter:^BOOL(JSCartModel *model) { if (!model.isSelect) { self.isSelectAll = NO; } return model.isSelect; }] map:^id(JSCartModel *model) { return @(model.p_quantity*model.p_price); }]; }] array]; for (NSArray *priceA in pricesArray) { for (NSNumber *price in priceA) { allPrices += price.floatValue; } } return allPrices; } |
全選yes or not
shopSelectArray轉流map定義需求isSelect為YES的物件然後return 成可變陣列.
cartData轉流map定義需求,對其引數value轉流map定義需求KVC使model的isSelect屬性為YES,再次計算記錄總價allPrices.return再return成可變陣列.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- (void)selectAll:(BOOL)isSelect{ __block float allPrices = 0; self.shopSelectArray = [[[[self.shopSelectArray rac_sequence] map:^id(NSNumber *value) { return @(isSelect); }] array] mutableCopy]; self.cartData = [[[[self.cartData rac_sequence] map:^id(NSMutableArray *value) { return [[[[value rac_sequence] map:^id(JSCartModel *model) { [model setValue:@(isSelect) forKey:@"isSelect"]; if (model.isSelect) { allPrices += model.p_quantity*model.p_price; } return model; }] array] mutableCopy]; }] array] mutableCopy]; self.allPrices = allPrices; [self.cartTableView reloadData]; } |
單行選擇處理方法
KVC設定model的isSelect為YES,做店鋪下商品選中滿判斷
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (void)rowSelect:(BOOL)isSelect IndexPath:(NSIndexPath *)indexPath{ NSInteger section = indexPath.section; NSInteger row = indexPath.row; NSMutableArray *goodsArray = self.cartData[section]; NSInteger shopCount = goodsArray.count; JSCartModel *model = goodsArray[row]; [model setValue:@(isSelect) forKey:@"isSelect"]; //判斷是都到達足夠數量 NSInteger isSelectShopCount = 0; for (JSCartModel *model in goodsArray) { if (model.isSelect) { isSelectShopCount++; } } [self.shopSelectArray replaceObjectAtIndex:section withObject:@(isSelectShopCount==shopCount?YES:NO)]; [self.cartTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone]; /*重新計算價格*/ self.allPrices = [self getAllPrices]; } |
單行數量處理方法
KVC處理model.再次呼叫getAllPrices方法計算總價
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (void)rowChangeQuantity:(NSInteger)quantity indexPath:(NSIndexPath *)indexPath{ NSInteger section = indexPath.section; NSInteger row = indexPath.row; JSCartModel *model = self.cartData[section][row]; [model setValue:@(quantity) forKey:@"p_quantity"]; [self.cartTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone]; /*重新計算價格*/ self.allPrices = [self getAllPrices]; } |
左滑刪除商品
陣列刪除,做店鋪下商品刪除完判斷處理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
- (void)deleteGoodsBySingleSlide:(NSIndexPath *)path{ NSInteger section = path.section; NSInteger row = path.row; NSMutableArray *shopArray = self.cartData[section]; [shopArray removeObjectAtIndex:row]; if (shopArray.count == 0) { /*1 刪除資料*/ [self.cartData removeObjectAtIndex:section]; /*2 刪除 shopSelectArray*/ [self.shopSelectArray removeObjectAtIndex:section]; [self.cartTableView reloadData]; } else { [self.cartTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone]; } self.cartGoodsCount-=1; /*重新計算價格*/ self.allPrices = [self getAllPrices]; } |
刪除選中處理方法
建立可變集合shopSelectIndex,遍歷cartData,遍歷shopArray,取得選中的index2加到selectIndexSet中,做店鋪count和選中商品相等判斷,index1加到shopSelectIndex中,cartGoodsCount做遞減處理,然後依次shopArray做刪除操作,cartData在迴圈外刪除操作,shopSelectArray在迴圈外刪除操作,價格為0,重新計算價格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- (void)deleteGoodsBySelect{ /*1 刪除資料*/ NSInteger index1 = -1; NSMutableIndexSet *shopSelectIndex = [NSMutableIndexSet indexSet]; for (NSMutableArray *shopArray in self.cartData) { index1++; NSInteger index2 = -1; NSMutableIndexSet *selectIndexSet = [NSMutableIndexSet indexSet]; for (JSCartModel *model in shopArray) { index2++; if (model.isSelect) { [selectIndexSet addIndex:index2]; } } NSInteger shopCount = shopArray.count; NSInteger selectCount = selectIndexSet.count; if (selectCount == shopCount) { [shopSelectIndex addIndex:index1]; self.cartGoodsCount-=selectCount; } [shopArray removeObjectsAtIndexes:selectIndexSet]; } [self.cartData removeObjectsAtIndexes:shopSelectIndex]; /*2 刪除 shopSelectArray*/ [self.shopSelectArray removeObjectsAtIndexes:shopSelectIndex]; [self.cartTableView reloadData]; /*3 carbar 恢復預設*/ self.allPrices = 0; /*重新計算價格*/ self.allPrices = [self getAllPrices]; } |
VC做監聽觀察處理
全選
1 2 3 4 |
[[self.cartBar.selectAllButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) { x.selected = !x.selected; [self.viewModel selectAll:x.selected]; }]; |
刪除
1 2 3 |
[[self.cartBar.deleteButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) { [self.viewModel deleteGoodsBySelect]; }]; |
結算
1 2 3 |
[[self.cartBar.balanceButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *x) { }]; |
觀察價格屬性
1 2 3 4 5 |
WEAK [RACObserve(self.viewModel, allPrices) subscribeNext:^(NSNumber *x) { STRONG self.cartBar.money = x.floatValue; }]; |
全選狀態
1 |
RAC(self.cartBar.selectAllButton,selected) = RACObserve(self.viewModel, isSelectAll); |
title購物車數量
1 2 3 4 5 6 7 8 9 |
[RACObserve(self.viewModel, cartGoodsCount) subscribeNext:^(NSNumber *x) { STRONG if(x.integerValue == 0){ self.title = [NSString stringWithFormat:@"購物車"]; } else { self.title = [NSString stringWithFormat:@"購物車(%@)",x]; } }]; |
總結
主要的方法我都一一講了很清楚,具體的怎麼呼叫,可以到service裡,cell裡,控制元件裡看,寫了這麼一大堆,如果你還有什麼不懂,或者有更好的建議請留言,或者到我的github上issue我,如果我寫的對你有些幫助,請給予辛勞的博主一些打賞,謝謝~