iOS 购物车的全选,单选的实现方法

类似淘宝的购物车类型

实现方法有千万种,我使用了字典数据key不能重复的特性,当然集合也具备这个特性,只要具备这个特性的都可以使用这种思路实现购物车的全选,

我定义了一个数组listArray 存储所有的数据类型,和一个selectDict选中的商品字典类型

selectDict存储数据类型大概是这样


将选中cell的indexPath设为字典的key,以为key不能重复,因此不需要担心会有重复的可能。

只要在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section方法中对字典的

[dict allValues].count == 商品的数量  就可以知道是否是全部选中。


#下面是部分实现源码

//判断是否是全部选中

- (BOOL)JudgementSelectAll:(NSIndexPath *)indexPath{

    NSDictionary *dict =EncodeDicFromDic(self.selectDict, [NSStringstringWithFormat:@"%ld",indexPath.section]);

    NSInteger tag =1000 + indexPath.section;

//     MyCartHeadView *view = [self.view viewWithTag:tag];

    if ([dictallValues].count ==EncodeArrayFromDic(self.listArray[indexPath.section],@"cartList").count) {

//        view.selectedBtn.selected = YES;

        returnYES;

    }else{

//        view.selectedBtn.selected = NO;

        returnNO;

    }

}

//sectionView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section <self.listArray.count) {

        MyCartHeadView *view = [[[NSBundlemainBundle] loadNibNamed:@"MyCartHeadView"owner:niloptions:nil]lastObject];

        view.section = section;

        __blocktypeof(self)weakSelf =self;

        view.selectBlock = ^(NSInteger section,BOOL select){

            if (select) {

                NSMutableDictionary *dict =EncodeMutableDicFromDic(self.selectDict, [NSStringstringWithFormat:@"%ld",section]);

                if (!dict) {

                    dict = [NSMutableDictionarydictionary];

                }

                for (int i =0; i < EncodeArrayFromDic(weakSelf.listArray[section],@"cartList").count; i++) {

                    [dict setValue:@"1"forKey:[NSStringstringWithFormat:@"%d",i]];

                }

                [weakSelf.selectDictsetValue:dict forKey:[NSStringstringWithFormat:@"%ld",section]];

            }else{

                [weakSelf.selectDictsetValue:nilforKey:[NSStringstringWithFormat:@"%ld",section]];

            }

            [weakSelf.tableViewreloadData];

        };

        view.selectedBtn.selected = [selfJudgementSelectAll:[NSIndexPathindexPathForRow:0inSection:section]];

        return view;

    }

    returnnil;

}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在UniApp中实现购物车全选单选和反选功能,可以通过以下步骤进行操作: 1. 数据结构设计:首先,你需要定义一个购物车数据结构,可以使用数组或对象来表示每个商品的信息,例如商品名称、价格、数量等。同时,为每个商品添加一个选中状态的属性,用于记录是否被选中。 2. 渲染购物车列表:在页面中使用`v-for`指令遍历购物车数据,并将商品信息展示出来。同时,为每个商品的选中状态绑定一个`v-model`指令,用于实现单选功能。 3. 实现全选功能:添加一个全选的复选框,通过绑定一个变量来控制全选的状态。当全选复选框被点击时,遍历购物车数据,将每个商品的选中状态与全选状态保持一致。 4. 实现反选功能:添加一个反选按钮,当点击反选按钮时,遍历购物车数据,将每个商品的选中状态取反。 5. 计算总价和总数量:通过遍历购物车数据,累加选中商品的价格和数量,得到总价和总数量。 6. 相关代码示例: ```html <template> <div> <div> <input type="checkbox" v-model="selectAll" @change="handleSelectAll" /> 全选 <button @click="handleInverseSelect">反选</button> </div> <div v-for="(item, index) in cartList" :key="index"> <input type="checkbox" v-model="item.selected" @change="handleSelectItem(index)" /> {{ item.name }} - ¥{{ item.price }} - 数量:{{ item.quantity }} </div> <div> 总价:¥{{ totalPrice }},总数量:{{ totalQuantity }} </div> </div> </template> <script> export default { data() { return { selectAll: false, cartList: [ { name: '商品1', price: 10, quantity: 1, selected: false }, { name: '商品2', price: 20, quantity: 2, selected: false }, { name: '商品3', price: 30, quantity: 3, selected: false } ] }; }, computed: { totalPrice() { let total = 0; for (let item of this.cartList) { if (item.selected) { total += item.price * item.quantity; } } return total; }, totalQuantity() { let total = 0; for (let item of this.cartList) { if (item.selected) { total += item.quantity; } } return total; } }, methods: { handleSelectAll() { for (let item of this.cartList) { item.selected = this.selectAll; } }, handleSelectItem(index) { this.cartList[index].selected = !this.cartList[index].selected; }, handleInverseSelect() { for (let item of this.cartList) { item.selected = !item.selected; } } } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值