iOS 仿链家筛选(单选、多选、滑动筛选联动、多表联动)

目前市场上很多应用都包含了筛选功能,自己写了个demo给大家分享一下,共同学习,共同进步

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 重置密码http://sc.chinaz.com/info/140328474587.htm CocoaPods第...
    xwlyun阅读 2,157评论 2 5
  • QQ 的消息界面,cell 滑动出现删除、标记已读、置顶按钮,这里只是做一个高仿的布局页面。 拆解 分析 QQ 消...
    redye阅读 1,386评论 7 14
  • 1 .控制器的基类 - (void)viewDidLoad { [super viewDidLoad];...
    随心忧阅读 339评论 0 0
  • UIView可以说是我们日常工作中接触最多的一个对象、是所有视图控件(不包括视图控制器)的基类。主要的功能包括视图...
    kirito_song阅读 4,047评论 1 33
  • UIView - 视图 UIView为屏幕上的矩形区域管理内容的对象,一般称为视图(以下称为视图)。视图是应用程序...
    寻心_0a46阅读 591评论 0 1