目前市场上很多应用都包含了筛选功能,自己写了个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添加到选中的数组中
[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从选中的数组中移除
[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、实现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多表联动
1、首先创建三个tableView和三个数据源数组
2、实现tableView的dataSource方法
//返回指定组的行数
- (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的数据赋值并取出相应的model,然后刷表
#pragma mark - 点击右侧 cell 的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *currentIndex = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
// 点击省出现市
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 {
// 把省对应的市的数组给属性赋值
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"]) { // 点击区
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