<h5>
本人刚开始写简书不久有很多不足之道请包涵
</h5>
<h5>
购物车
</h5>
参考源码
<h5>
特别想把所有逻辑都写在模型中但是还是少不了与VC的交互,购物车无非 就是单选,组选,全选 三个状态的相互关联,我在每个组的模型中与每个item的模型中添加一个BOOL值用来记录按钮的选中状态. 代码如下
</h5>
<pre>
import <Foundation/Foundation.h>
@class shopJsonModel,shopJsonListModel;
@interface shopBaseModel : NSObject
@property (nonatomic,copy) NSString * message;
@property (nonatomic,strong) NSArray * json;
@end
@interface shopJsonModel : NSObject
@property (nonatomic,copy) NSString * shop_name;
@property (nonatomic,strong) NSArray * list;
// 记录组头按钮是否被选择的状态
@property (nonatomic,assign) BOOL isGroupSelect;
@end
@interface shopJsonListModel : NSObject
@property (nonatomic,copy) NSString * goods_name;
@property (nonatomic,copy) NSString * goods_imgurl;
@property (nonatomic,copy) NSString * cash;
// 每个Item上的按钮是否被选择
@property (nonatomic,assign) BOOL isItemSelect;
@end
</pre>
<h6>
下面是几种状态的代理方法,暂时没想到如何在模型中去写这些逻辑
</h6>
<pre>
pragma mark - 全选
- (void)allButtonSelect:(shopButton *)allButton{
[self didAllSelect:allButton.selected];
[self judgeShopMoney];
} - (void)didAllSelect:(BOOL)isAll{
for (int i = 0 ; i < self.shopModel.json.count; i++) {
shopJsonModel *json = self.shopModel.json[i];
json.isGroupSelect = isAll;
for (int y = 0; y < json.list.count; y++) {
shopJsonListModel *itemModel = json.list[y];
itemModel.isItemSelect = isAll;
}
}
[self.tbView reloadData];
}
pragma mark - 组选
/**
组选
@param group 每一组的按钮
*/
- (void)didSelectgroup:(shopButton *)group andSection:(NSInteger)section{
[self setSelectGroup:group.selected andSection:section];
[self isAllButtonSelect];
[self judgeShopMoney];
} - (void)setSelectGroup:(BOOL)isGroup andSection:(NSInteger)section{
shopJsonModel *jsonModel = self.shopModel.json[section];
for (shopJsonListModel *listModel in jsonModel.list) {
listModel.isItemSelect = isGroup;
}
[self.tbView reloadData];
}
pragma mark - 单选
/**
选择单独一个商品
@param shop 每一个商品的按钮
*/
- (void)singleButton:(shopButton *)shop andRowAtIndexPath:(NSIndexPath *)indexPath{
BOOL isGroup = YES;
shopJsonModel *jsonModel = self.shopModel.json[indexPath.section];
for (shopJsonListModel *itemModel in jsonModel.list) {
if (!itemModel.isItemSelect) {
isGroup = NO;
}
}
jsonModel.isGroupSelect = isGroup;
[self isAllButtonSelect];
[self.tbView reloadData];
[self judgeShopMoney];
}
/**
判断所有的组头是否被选择
*/ - (void)isAllButtonSelect{
BOOL isAll = YES;
for (int i = 0; i < self.shopModel.json.count; i++) {
shopJsonModel *m = self.shopModel.json[i];
if (!m.isGroupSelect) {
isAll = NO;
}
}
self.allBgView.isAllSelect = isAll;
}
/**
计算商品的价格
*/
-(void)judgeShopMoney{
double allPrice = 0;
for (shopJsonModel *json in self.shopModel.json) {
for (shopJsonListModel *item in json.list) {
if (item.isItemSelect) {
allPrice += [item.cash doubleValue];
}
}
}
self.allBgView.price=[NSString stringWithFormat:@"%.02f",allPrice];
NSLog(@"%f",allPrice);
}
</pre>
<h5>
如果想要获取哪一个被选中哪一个没有被选中 遍历一下self.shopModel.json 数组.计算商品价格等. 代码如下
</h5>
<pre>
/**
计算商品的价格
*/
-(void)judgeShopMoney{
double allPrice = 0;
for (shopJsonModel *json in self.shopModel.json) {
for (shopJsonListModel *item in json.list) {
if (item.isItemSelect) {
allPrice += [item.cash doubleValue];
}
}
}
self.allBgView.price=[NSString stringWithFormat:@"%.02f",allPrice];
NSLog(@"%f",allPrice);
}
</pre>