类似淘宝的购物车类型
实现方法有千万种,我使用了字典数据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;
}