目前市場上很多應用都包含了篩選功能,自己寫了個demo給大家分享一下,共同學習,共同進步

1.gif
一、先說說常見的collectionView多選功能
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
LBFilterModel* filterModel = [self.dataSource objectAtIndex:indexPath.section];
LBFilterListModel* listModel = filterModel.filterList[indexPath.item];
// 改變model選中屬性
listModel.isSelected = !listModel.isSelected;
// 刷新indexPath所在行
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects: indexPath, nil]];
// 將選中的model添加到選中的數(shù)組中
[self.selectedArray addObject:listModel];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
LBFilterModel* filterModel = [self.dataSource objectAtIndex:indexPath.section];
LBFilterListModel* listModel = filterModel.filterList[indexPath.item];
listModel.isSelected = !listModel.isSelected;
[collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects: indexPath, nil]];
// 將選中的model從選中的數(shù)組中移除
[self.selectedArray removeObject:listModel];
}

1.jpg
二、UICollectionView與UITapGestureRecognizer沖突
1、給背景view添加手勢識別
UITapGestureRecognizer*pan = [[UITapGestureRecognizer alloc] initWithTarget:hud action:@selector(touchUpbgView)];
pan.delegate = self;
[hud addGestureRecognizer:pan];
2、添加手勢識別代理
@interface LBFilterView() <UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate>
3、實現(xiàn)UIGestureRecognizerDelegate方法
//判斷如果點擊的是collectionView/tableView的cell,就把手勢給關閉了 return NO;否則手勢存在 return YES;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ( [NSStringFromClass([touch.view class]) isEqualToString:@"UIView"]) {
return NO;
}
return YES;
}
三、tableView多表聯(lián)動
1、首先創(chuàng)建三個tableView和三個數(shù)據(jù)源數(shù)組
2、實現(xiàn)tableView的dataSource方法
//返回指定組的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.provinceTableView == tableView) {
return self.provinceArr.count;
} else if (self.cityTableView == tableView) {
return self.cityArr.count;
} else
return self.zoneArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.provinceTableView == tableView) {
static NSString *cellId = @"provinceCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
LBAddressModel * addressModel = self.provinceArr[indexPath.row];
cell.textLabel.text = addressModel.name;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.backgroundColor = self.provinceModel == self.provinceArr[indexPath.row] ? LBUIColorWithRGB(0x4CB371, .9) : LBUIColorWithRGB(0xDCDCDC, 1);
return cell;
} else if (self.cityTableView == tableView) {
static NSString *cellId = @"cityCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
LBAddressModel * cityModel = self.cityArr[indexPath.row];
cell.textLabel.text = cityModel.name;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.backgroundColor = self.cityModel == self.cityArr[indexPath.row] ? LBUIColorWithRGB(0x4CB371, .9) : LBUIColorWithRGB(0xE8E8E8, 1);
return cell;
} else if (self.zoneTableView == tableView) {
static NSString *cellId = @"zoneCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.backgroundColor = [self.nowSelectObjectArr containsObject:self.zoneArr[indexPath.row]]?LBUIColorWithRGB(0x4CB371, .9):LBUIColorWithRGB(0xF5F5F5, 1);
LBAddressModel * zoneModel = self.zoneArr[indexPath.row];
cell.textLabel.text = zoneModel.name;
cell.textLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
3、單擊tableViewCell的時候,將childTableView的數(shù)據(jù)賦值并取出相應的model,然后刷表
#pragma mark - 點擊右側 cell 的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *currentIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
// 點擊省出現(xiàn)市
if (tableView == self.provinceTableView) {
LBAddressModel* model = self.provinceArr[currentIndex.row];
[self showTableWithIndex:indexPath type:@"city" model:model];
} else if (tableView == self.cityTableView) {
LBAddressModel* model = self.cityArr[currentIndex.row];
[self showTableWithIndex:indexPath type:@"zone" model:model];
} else if (tableView == self.zoneTableView) {
LBAddressModel* model = self.zoneArr[currentIndex.row];
[self showTableWithIndex:indexPath type:@"done" model:model];
}
}
- (void)showTableWithIndex:(NSIndexPath*)indexPath type:(NSString*)type model:(LBAddressModel*)model {
// 把省對應的市的數(shù)組給屬性賦值
if ([type isEqualToString:@"city"]) { // 點擊省
self.cityArr = [NSMutableArray arrayWithArray:model.cityList];
self.provinceModel = model;
[self.provinceTableView reloadData];
[self.cityTableView reloadData];
[self changeFrameWithType:@"city"];
} else if ([type isEqualToString:@"zone"]) { // 點擊市
self.zoneArr = [NSMutableArray arrayWithArray:model.areaList];
[self.cityTableView reloadData];
[self.zoneTableView reloadData];
self.cityModel = model;
[self changeFrameWithType:@"zone"];
[self.nowSelectObjectArr removeAllObjects];
} else if ([type isEqualToString:@"done"]) { // 點擊區(qū)
if ([self.nowSelectObjectArr containsObject:self.zoneArr[indexPath.row]]) {
[self.nowSelectObjectArr removeObject:self.zoneArr[indexPath.row]];
}else{
[self.nowSelectObjectArr addObject:self.zoneArr[indexPath.row]];
}
[self.zoneTableView reloadData];
}
}
- (void)changeFrameWithType:(NSString*)type {
UITableView *tableView = [type isEqualToString:@"city"]?self.provinceTableView:self.cityTableView;
UITableView* childView = [type isEqualToString:@"city"]?self.cityTableView:self.zoneTableView;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 animations:^{
[tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([type isEqualToString:@"city"]?LBScreenW/4:(LBScreenW/4*3)/5*2);
}];
[tableView.superview layoutIfNeeded];//強制繪制
[childView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([type isEqualToString:@"city"]?LBScreenW/4*3:(LBScreenW/4*3)/5*3);
}];
[childView.superview layoutIfNeeded];//強制繪制
}];
});
}
4、順便說下masonry動畫
使用的是masonry的updateConstraints方法
[UIView animateWithDuration:0.3 animations:^{
[tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/4);
}];
[tableView.superview layoutIfNeeded]; //強制繪制
}];
重點在 [tableView.superview layoutIfNeeded]句,需要調用所變化view的superView

2.jpg