美文网首页iOS开发iOS技术资料iOS Developer
iOS开发 -- 简化购物车多选&全选功能 Tips

iOS开发 -- 简化购物车多选&全选功能 Tips

作者: Jeavil_Tang | 来源:发表于2017-06-05 20:33 被阅读1471次

有这样一个界面,类似与淘宝的购物车功能,但要比购物车简单,没有删除和编辑功能。

样图.JPG

下面我们先理一理实现该功能的思路吧!

  • 一眼望去,这是一个 Group 类型的 TablevVew ,每个 section 里面含有许多的cell
  • 每一个section可以展开和收起
  • 对于每个cell的选择:
    • 勾选任意行
    • 勾选人意组
    • 全选
    • 选中某一组中的全部 cell
    • 选中某一组后取消选中组中的某一cell
    • 全选后取消选中某一行
    • 全选后取消选中某一组
      大概功能就这么多,其中最重要的就是,如何将每一组中的所有cell和当前组建立联系,当分别选中某一组中的所有cell后,当前组也为选中状态,而取消任何选中一个cell,当前组也为为选中状态。

解决方案:

  • 首先,创建GroupModel,该模型负责存储每个组中的数据,还有选中状态,这里我定义为 groupIsSelected;
@interface UnpaidGroupItem : NSObject

@property (nonatomic, strong) NSString *Date;
@property (nonatomic, strong) NSString *ForPaymentAllPrice;
@property (nonatomic, strong) NSArray  *SecondList;
@property (nonatomic, assign) BOOL groupIsSelected ;  // 当前group全选按钮是否选中

@end
  • 其次,创建每个cell中的数据模型,当然也需要一个选中状态,这里我定义为 itemIsSelected
@interface UnpaidListModel : NSObject

@property (nonatomic, strong) NSString *TicketNo;
@property (nonatomic, strong) NSString *TicketAccount;
@property (nonatomic, strong) NSString *ForPaymentDetialName;
@property (nonatomic, strong) NSString *Time;
@property (nonatomic) UnpaidTicketType TicketType;
@property (nonatomic, assign) BOOL itemIsSelected; // 是否选中该商品

@end

之所以把选中状态存入模型,为了方便存取当前cell的选中状态

  • 将当前组和其中的cell绑定在一起:利用属性和代理 (当某个section被选中时,其中的所有cell也被选中)
    • 在 groupcell 中添加一个 sectionIndex,用来记录是哪个组
    • 定义一个协议,当点击到section中的选择按钮时,将当前的sectionIndex 作为参数,传递给实现协议一方。这样当实现协议的对象,拿到当前的sectionIndex后,就可以便利当前section中的数据,将当前组中的所有 itemIsSelected = Yes;
#pragma mark - CreateNewPaymentGroupCellDelegate

- (void)groupSelected:(NSInteger)sectionIndex {
    UnpaidGroupItem *group = self.dataArray[sectionIndex];
    group.groupIsSelected = !group.groupIsSelected;
    [group.SecondList enumerateObjectsUsingBlock:^(UnpaidListModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.itemIsSelected = group.groupIsSelected;
    }];
    
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionIndex];
    [self.tableView reloadSections:indexSet withRowAnimation:NO];
    [self.tableView expandSection:sectionIndex animated:YES];
    self.selectAllBtn.selected = [self isAllTicketChoosed];
    self.totalAccount.text = [NSString stringWithFormat:@"¥%.2f",[self countTotalPrice]];
}
  • 当前组中的cell全部选中时,section上的选中按钮也变为选中状态:委托代理
    • 在cell中定义一个cell属性,并定义协议和代理,当点击cell中的
      选中按钮时,将当前 cell 和 button.select状态作为参数传递给遵循协议的controller,这样controller可以获取到该cell所处的位置,来取得当前所处的section。然后通过循环遍历section中所有数据的itemIsSelected的状态,比较选中状态的cell 和 该section 中的数组的cout是否相等,如果相等,则将 groupItem.groupIsSelected = YES; 否则 groupItem.groupIsSelected = NO; 之后再刷新tableview
- (void)ticketSelected:(CreateNewPaymentTableViewCell *)cell isSelected:(BOOL)selected {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    UnpaidGroupItem *groupItem = self.dataArray[indexPath.section];
    UnpaidListModel *model = groupItem.SecondList[indexPath.row - 1] ;
    model.itemIsSelected = !model.itemIsSelected;
    __block NSInteger count = 0;
    [groupItem.SecondList enumerateObjectsUsingBlock:^(UnpaidListModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.itemIsSelected) {
            count ++;
        }
    }];
    
    if (count == groupItem.SecondList.count)
        groupItem.groupIsSelected = YES;
    else
        groupItem.groupIsSelected = NO;
    
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:NO];
    self.selectAllBtn.selected = [self isAllTicketChoosed];
    self.totalAccount.text = [NSString stringWithFormat:@"¥%.2f",[self countTotalPrice]];
}

  • 全选,只需要检查数据源中,所有组的groupIsSelected 状态,如果所有组中的groupIsSelected 都为YES,则说明所有数据都被选中
- (IBAction)onClickSelectAllBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    for (UnpaidGroupItem *group in self.dataArray) {
        group.groupIsSelected = sender.selected;
        for (UnpaidListModel *model in group.SecondList) {
            model.itemIsSelected = group.groupIsSelected;
        }
    }
   }
#pragma mark - 判断是否全部选中了
- (BOOL)isAllTicketChoosed
{
    if (_dataArray.count == 0) {
        return NO;
    }
    NSInteger count = 0;
    for (UnpaidGroupItem *groupItem in _dataArray) {
        if (groupItem.groupIsSelected) {
            count ++;
        }
    }
    return (count == _dataArray.count);
}

OK,这就是实现该功能的简单思路。

总结:

  1. 首先,想办法将每一个section中的选中状态存储起来,再把每个cell中的选中状态存储起来
  2. 将所选的section和section下面所有的cell帮顶起来,可以通过section将其下的所有cell数据都可以取到
  3. 获取到选中的cell位置,这样可以取得cell究竟归属于哪个section
    只要掌握着三点,购物车功能的实现就简单多了。

相关文章

网友评论

    本文标题:iOS开发 -- 简化购物车多选&全选功能 Tips

    本文链接:https://www.haomeiwen.com/subject/uqjwfxtx.html