iOS 仿鏈家篩選(單選、多選、滑動篩選聯(lián)動、多表聯(lián)動)

目前市場上很多應用都包含了篩選功能,自己寫了個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添加到選中的數(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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 重置密碼http://sc.chinaz.com/info/140328474587.htm CocoaPods第...
    xwlyun閱讀 2,205評論 2 5
  • QQ 的消息界面,cell 滑動出現(xiàn)刪除、標記已讀、置頂按鈕,這里只是做一個高仿的布局頁面。 拆解 分析 QQ 消...
    redye閱讀 1,438評論 7 14
  • 1 .控制器的基類 - (void)viewDidLoad { [super viewDidLoad];...
    隨心憂閱讀 364評論 0 0
  • UIView可以說是我們日常工作中接觸最多的一個對象、是所有視圖控件(不包括視圖控制器)的基類。主要的功能包括視圖...
    kirito_song閱讀 4,171評論 1 33
  • UIView - 視圖 UIView為屏幕上的矩形區(qū)域管理內容的對象,一般稱為視圖(以下稱為視圖)。視圖是應用程序...
    尋心_0a46閱讀 734評論 0 1

友情鏈接更多精彩內容