首頁圖.png
沒有用到通知.只要兩個代理方法實現購物車
效果展示:
購物車.gif
二. 簡介:
1.第三方:用到的框架SDWebImage/MJExtension,應該都很熟悉,第一個是圖片下載,第二個是字典轉模型.
2.功能: ①勾選總價計算; ②刪除功能
三. 使用:
0.拖入相關文件
#import "ViewController.h"
#import "MKShopCarController.h"
#import "MKOrderListModel.h"
#import "MJExtension.h"
1.傳遞數據
// 傳遞模型數據
- (void)shopCarClick {
MKShopCarController * shop = [MKShopCarController new];
shop.modelList = _nmArray;
[self.navigationController pushViewController:shop animated:YES];
}
2.修改模型數據
修改模型key值.png
@end
@end
四. 代碼實現
- 1
// 繼承UIViewController
@interface MKShopCarController : UIViewController
// 設置表格
- (void)setTableList {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
UINib * nib = [UINib nibWithNibName:@"MKShopCarCell" bundle:nil];
[_tableView registerNib:nib forCellReuseIdentifier:@"shop"];
_tableView.rowHeight = 110;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
}
這里創建為UITableViewStylePlain樣式組頭會停住
UITableViewStyleGroup樣式組頭不會停住,這也是分組樣式與否的區別
- 2.第一個代理方法,自定義cell中,選中按鈕的點擊事件
并且將當前的cell的tag值傳遞回控制器中
//自定義cell中
@interface MKShopCarCell : UITableViewCell
// 自定義cell中左側選中按鈕的點擊事件
- (IBAction)fabricSelectClick:(UIButton*)sender {
_goodsModel.isSelected = !_goodsModel.isSelected;
fabricSelectedBtn.selected = !fabricSelectedBtn.selected;
// NSLog(@"%zd",self.tag);
if ([self.shopDelegate respondsToSelector:@selector(shopCellSelectedClick:)]) {
[self.shopDelegate shopCellSelectedClick:self.tag];
}
}
- 3.第二個代理方法,自定義組頭中,選中按鈕的點擊事件中
同樣將當前section中tag傳遞
//自定義組頭中
@interface MKHeaderFooterView : UITableViewHeaderFooterView
// 組頭的點擊事件
- (void)headerBtnClick: (UIButton*)HeaderBtn :(NSInteger)section{
HeaderBtn.selected = !HeaderBtn.selected;
if ([self.headerDelegate respondsToSelector:@selector(headerSelectedBtnClick:)]) {
[self.headerDelegate headerSelectedBtnClick:self.tag];
}
}
- 4.控制器中,遵守協議,實現這兩個代理方法
<UITableViewDelegate,UITableViewDataSource,shopCarCellDelegate,headerViewDelegate>
// 注冊cell/header 或者判斷為空的時候 創建
UINib * nib = [UINib nibWithNibName:@"MKShopCarCell" bundle:nil];
[_tableView registerNib:nib forCellReuseIdentifier:@"shop"];
- 組頭的數據方法 | 綁定tag
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
_headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MKShopCarHeader"];
if (_headerView == nil) {
_headerView = [[MKHeaderFooterView alloc] init];
_headerView.headerDelegate = self;
}
MKOrderListModel*listModel = _modelList[section];
_headerView.tag = section;
_headerView.headerBtn.selected = listModel.groupSelected;
return _headerView;
}
- cell的數據源方法 | 綁定tag
// cell顯示內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MKShopCarCell*shopCell = [tableView dequeueReusableCellWithIdentifier:@"shop"];
shopCell.shopDelegate = self;
//取出對應組的對應商品信息
shopCell.goodsModel = ((MKOrderListModel*)_modelList[indexPath.section]).goods[indexPath.row];
// 給cell做標記
shopCell.tag = (long)indexPath.section *100 + (long)indexPath.row;
// if (_modelList.count != self.dic.count) {
NSString * cellTag = [NSString stringWithFormat:@"%zd",shopCell.tag];
NSDictionary* _tempDic = @{
cellTag:indexPath
};
[self.dic addEntriesFromDictionary:_tempDic];
// }
return shopCell;
}
這里將所有的indexpath存入一個字典里,并通過對應的tag作為key值
為保證唯一性section和row都做了處理作為key
- 實現cell中的代理方法 | 計算選中與未選中的總價
#pragma mark - cell上的代理方法獲 | 取的價格
- (void)shopCellSelectedClick:(NSInteger)shopCellTag {
//判斷組的是否選中狀態是否修改
NSString * cellTagStr = [NSString stringWithFormat:@"%zd",shopCellTag];
NSIndexPath *indexPath = self.dic[cellTagStr];
MKOrderListModel * listModel = (MKOrderListModel*)_modelList[indexPath.section];
//0.便利當前組cell上選中按鈕的個數
NSInteger seletedNum =0;
for (MKGoodsModel* goodsModel in listModel.goods) {
if (goodsModel.isSelected) {
seletedNum += 1;
}
// 1.當前組的cell的個數 是否等于 勾選的總數
if (((MKOrderListModel*)_modelList[indexPath.section]).goods.count == seletedNum) {
listModel.groupSelected = YES;
} else {
listModel.groupSelected = NO;
}
[_tableView reloadData];
}
MKGoodsModel *goodsModel = ((MKOrderListModel*)_modelList[indexPath.section]).goods[indexPath.row];
float shop_price = goodsModel.shop_price;
float goods_number = goodsModel.goods_number;
if (!goodsModel.isSelected) {
_totalNum = _totalNum - shop_price*goods_number;
}else {
_totalNum = _totalNum + shop_price*goods_number;
}
_hejiLabel.text = [NSString stringWithFormat:@"¥%.2f",_totalNum -1 + 1];
}
即,根據代理中的tag 取出 indexpath 再根據indexpath 取出模型中的數據
另外判斷本組中的cell都選中時候組的選中狀態也為選中!
- 實現header中我們寫的的代理方法 | 計算選中與未選中的總價
#pragma mark - 代理方法組頭header的選中狀態
- (void)headerSelectedBtnClick:(NSInteger)section {
// NSLog(@"%zd",section);
MKOrderListModel*listModel = _modelList[section];
listModel.groupSelected = !listModel.groupSelected;
// 判斷如果點擊 | header選中
if (listModel.groupSelected) {
for (MKGoodsModel* goodsModel in listModel.goods) {
if (!goodsModel.isSelected) { //下面不是選中狀態的cell 將價格加入到總價當中
float shop_price = goodsModel.shop_price; //價格
float goods_number = goodsModel.goods_number; // 數量
_totalNum += shop_price * goods_number;
goodsModel.isSelected = YES;
}
}
} else { // 取消header選中 所有都取消
for (MKGoodsModel* goodsModel in listModel.goods) {
goodsModel.isSelected = NO;
float shop_price = goodsModel.shop_price; //價格
float goods_number = goodsModel.goods_number; // 數量
_totalNum -= shop_price * goods_number;
}
}
// NSLog(@"總價格為: %.2f",_totalNum);
_hejiLabel.text = [NSString stringWithFormat:@"¥%.2f",_totalNum - 1 + 1];
[_tableView reloadData];
}
邏輯分析: 組頭點擊選中 : 則遍歷本組cell 未選中狀態的cell計算價格加入到總價更并改cell選中狀態
組頭點擊未選中 : 遍歷本組中cell 的價格 從總價中減去并更改cell的選中狀態
最后reloadData
- 刪除方法 | 根據方法中自帶的indexpath從數據中刪除跟新即可
//左拉抽屜(刪除和修改按鈕)
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一個刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
MKOrderListModel*listModel = _modelList[indexPath.section];
NSMutableArray*goodsModel = (NSMutableArray*)listModel.goods;
/// 如果刪除的是帶勾選的則計算一次數值
MKGoodsModel*goodModel = (MKGoodsModel*)goodsModel[indexPath.row];
if (goodModel.isSelected) {
float shop_price = goodModel.shop_price; //價格
float goods_number = goodModel.goods_number; // 數量
_totalNum -= shop_price * goods_number;
_hejiLabel.text = [NSString stringWithFormat:@"%.2f",_totalNum];
}
[goodsModel removeObjectAtIndex:indexPath.row]; // 刪除操作放到最后
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (goodsModel.count == 0) {
NSMutableArray *temp = [NSMutableArray arrayWithArray:_modelList];
[temp removeObjectAtIndex:indexPath.section];
_modelList = temp;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[tableView reloadData];
});
}];
// 修改資料按鈕
UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"修改"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
}];
editRowAction.backgroundColor = [UIColor blueColor];
// 將設置好的按鈕放到數組中返回
return @[deleteRowAction, editRowAction];
}
注意點 : ①可能遇到刪除報錯的情況,需要條件到一個臨時的可變數組中做刪除操作 再把刪除后的數組回傳
②判斷刪除cell中的選中,如果選中狀態則在總價中減去此價格即可
③刪除操作這里需要增加接口后臺數據做刪除
純手打,如果有用希望多多支持!~