首頁圖
上次寫完購物車精簡版被編輯推薦到了首頁,得到許多朋友關(guān)注,評論區(qū)小伙伴建議做一個完整版.于是利用閑暇時間新增了一些功能:
- 數(shù)量加減
- 可點擊textField修改,也可以加減按鈕修改
- 全選按鈕
github代碼:https://github.com/zhYes/YSShoppingCar-Complite.git
全選按鈕邏輯效果:
全選效果展示
part one 全選按鈕有關(guān)代碼
代理方法1中偽代碼:
#pragma mark - cell上的代理方法
- (void)shopCellSelectedClick:(NSInteger)shopCellTag {
....
///插入判斷代碼
//判斷 //cell改變組頭 //組頭改變?nèi)x
NSInteger selectedNum = 0 ;
for (MKOrderListModel * tempListModel in _modelArray) {//遍歷所有組
if (tempListModel.groupSelected) {//如果組頭是選中的
selectedNum += 1;
}
if (selectedNum == _modelArray.count) {
allSelectBtn.selected = YES;
}
}
} else {
listModel.groupSelected = NO;
allSelectBtn.selected = NO;
}
....
}
cell的選中增加了判斷代碼,決定全選按鈕的狀態(tài)
完整代碼:
#pragma mark - cell上的代理方法獲 | 取的價格
- (void)shopCellSelectedClick:(NSInteger)shopCellTag {
//判斷組的是否選中狀態(tài)是否修改
NSString * cellTagStr = [NSString stringWithFormat:@"%zd",shopCellTag];
NSIndexPath *indexPath = self.dic[cellTagStr];
MKOrderListModel * listModel = (MKOrderListModel*)_modelArray[indexPath.section];
//0.便利當(dāng)前組cell上選中按鈕的個數(shù)
NSInteger seletedNum =0;
for (MKGoodsModel* goodsModel in listModel.goods) {
if (goodsModel.isSelected) {
seletedNum += 1;
}
// 1.當(dāng)前組的cell的個數(shù) 是否等于 勾選的總數(shù)
if (((MKOrderListModel*)_modelArray[indexPath.section]).goods.count == seletedNum) {
listModel.groupSelected = YES; //cell改變組頭變?yōu)檫x中
//判斷 //cell改變組頭 //組頭改變?nèi)x
NSInteger selectedNum = 0 ;
for (MKOrderListModel * tempListModel in _modelArray) {//遍歷所有組
if (tempListModel.groupSelected) {//如果組頭是選中的
selectedNum += 1;
}
if (selectedNum == _modelArray.count) {
allSelectBtn.selected = YES;
}
}
} else {
listModel.groupSelected = NO;
allSelectBtn.selected = NO;
}
[_tableView reloadData];
}
MKGoodsModel *goodsModel = ((MKOrderListModel*)_modelArray[indexPath.section]).goods[indexPath.row];
float goods_price = goodsModel.goods_price;
float goods_number = goodsModel.goods_number;
if (!goodsModel.isSelected) {
_totalNum = _totalNum - goods_price*goods_number;
// NSLog(@"%.2f",_totalNum);
}else {
_totalNum = _totalNum + goods_price*goods_number;
// NSLog(@"%.2f",_totalNum);
}
_hejiLabel.text = [NSString stringWithFormat:@"¥%.2f",_totalNum -1 + 1];
if ([_hejiLabel.text containsString:@"-"]) {
_hejiLabel.text = @"¥0.0";
}
}
當(dāng)前cell選中時候,當(dāng)前組頭也選中的時候,判斷當(dāng)前所有選中組的個數(shù)是否等于全部組數(shù)
代理方法2中偽代碼:
#pragma mark - 代理方法組頭header的選中狀態(tài)
- (void)headerSelectedBtnClick:(NSInteger)section {
...
插入判斷
// 判斷如果點擊 | header選中
if (listModel.groupSelected) {
// /// 判斷組頭的點擊改變?nèi)x按鈕
NSInteger tempGroupSelectNum = 0;
for (MKOrderListModel *model in _modelArray) {
if (model.groupSelected) {
tempGroupSelectNum ++;
}
if (tempGroupSelectNum == _modelArray.count) {
allSelectBtn.selected = YES;
}
}
...
}
數(shù)組的選中按鈕中增加判斷
完整代碼:
#pragma mark - 代理方法組頭header的選中狀態(tài)
- (void)headerSelectedBtnClick:(NSInteger)section {
// NSLog(@"%zd",section);
MKOrderListModel*listModel = _modelArray[section];
listModel.groupSelected = !listModel.groupSelected;
// 判斷如果點擊 | header選中
if (listModel.groupSelected) {
// /// 判斷組頭的點擊改變?nèi)x按鈕
NSInteger tempGroupSelectNum = 0;
for (MKOrderListModel *model in _modelArray) {
if (model.groupSelected) {
tempGroupSelectNum ++;
}
if (tempGroupSelectNum == _modelArray.count) {
allSelectBtn.selected = YES;
}
}
for (MKGoodsModel* goodsModel in listModel.goods) {
if (!goodsModel.isSelected) { //下面不是選中狀態(tài)的cell 將價格加入到總價當(dāng)中
float goods_price = goodsModel.goods_price; //價格
float goods_number = goodsModel.goods_number; // 數(shù)量
_totalNum += goods_price * goods_number;
goodsModel.isSelected = YES;
}
}
} else { // 取消header選中 所有都取消
//全選按鈕變?yōu)椴贿x中
allSelectBtn.selected = NO;
for (MKGoodsModel* goodsModel in listModel.goods) {
goodsModel.isSelected = NO;
float goods_price = goodsModel.goods_price; //價格
float goods_number = goodsModel.goods_number; // 數(shù)量
_totalNum -= goods_price * goods_number;
}
}
// NSLog(@"總價格為: %.2f",_totalNum);
_hejiLabel.text = [NSString stringWithFormat:@"¥%.2f",_totalNum - 1 + 1];
if ([_hejiLabel.text containsString:@"-"]) {
_hejiLabel.text = @"0";
}
[_tableView reloadData];
}
全選按鈕的點擊處理:
/// MARK: 全選的點擊事件
- (void)allSelectBtnClick: (UIButton*)allSelectedBtn{
allSelectedBtn.selected = !allSelectedBtn.selected; // 修改全選按鈕的狀態(tài)
if (allSelectedBtn.selected) { // 如果全選按鈕改變了為選中
for (int i = 0; i <_modelArray.count; i ++) {
MKOrderListModel * listModel = _modelArray[i];
if (!listModel.groupSelected) {//遍歷如果組不是選中狀態(tài)
[self headerSelectedBtnClick:i]; //模擬組頭點擊了一次
}
}
}else { // 全選按鈕改變?yōu)椴贿x中 那么所有商品都不選中!
for (int i = 0; i < _modelArray.count; i ++) { // 遍歷所有的組頭點擊
[self headerSelectedBtnClick:i];
}
}
}
點擊全選,按鈕狀態(tài)取反
取反后,全選按鈕是選中,則遍歷所有組把所有未選中的組頭做一次點擊操作
取反后,全選按鈕是未選中,則所有組頭做一次點擊操作即可
修改數(shù)量效果展示:
修改數(shù)量效果
part two 修改數(shù)量相關(guān)代碼:
關(guān)于購物車中textField的邏輯代碼可查看我的另一篇記用了12個if做的textField輸入框的變態(tài)需求
這里貼一下數(shù)量加減的代碼:
/// MARK:數(shù)量增加按鈕
- (IBAction)plusBtnClick {
if (_ex_buynum_texField.editing) {
return;
}
// 保存之前值
float beforeBuyNum = _goodsModel.goods_number;
// 判斷不能大于20
if (_ex_buynum_texField.text.floatValue == 20) {
return;
}
/// 修改模型數(shù)據(jù)
_ex_buynum_texField.text = [NSString stringWithFormat:@"%.1f",_ex_buynum_texField.text.floatValue + 0.5];
_goodsModel.goods_number = _ex_buynum_texField.text.floatValue;
if ([self.shopDelegate respondsToSelector:@selector(shopCellEndEditerClick:beforeBuyNum:)]) {
[self.shopDelegate shopCellEndEditerClick:self.tag beforeBuyNum:beforeBuyNum];
}
/// 傳遞給服務(wù)器 一會寫 $$$$$$$$
}
- (IBAction)reduceBtnClick {
if (_ex_buynum_texField.editing) {
return;
}
// 保存之前值
float beforeBuyNum = _goodsModel.goods_number;
// 判斷不能小于0.5
if (_ex_buynum_texField.text.floatValue == 0.5) {
return;
}
/// 修改模型數(shù)據(jù)
_ex_buynum_texField.text = [NSString stringWithFormat:@"%.1f",_ex_buynum_texField.text.floatValue - 0.5];
_goodsModel.goods_number = _ex_buynum_texField.text.floatValue;
if ([self.shopDelegate respondsToSelector:@selector(shopCellEndEditerClick:beforeBuyNum:)]) {
[self.shopDelegate shopCellEndEditerClick:self.tag beforeBuyNum:beforeBuyNum];
}
/// 傳遞給服務(wù)器 一會寫 $$$$$$$$
}
@end