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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,548评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,069评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,985评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,305评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,324评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,030评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,639评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,552评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,081评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,194评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,327评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,004评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,688评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,188评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,307评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,667评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,337评论 2 358

推荐阅读更多精彩内容

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