基于tableView的篩選view

項(xiàng)目中經(jīng)常會(huì)有這樣一個(gè)篩選的需求 :
1.一個(gè)全部button(以下簡稱allBtn) 2. 若干列表 button(以下簡稱littleBtn)
需求 : allBtn選中時(shí),所有l(wèi)ittleBtn也選中 ; allBtn取消時(shí),所有l(wèi)ittleBtn也取消 ; 部分(非全部)littleBtn選中時(shí),全部button不選中 ; 全部littleBtn選中時(shí), allBtn也選中 . 如圖是我最終實(shí)現(xiàn)的效果:

篩選view.gif

實(shí)現(xiàn)這種需求的基本思路就是要添加兩個(gè)屬性 1. littleBtn是否點(diǎn)擊的 2. littleBtn在父視圖的位置, 這里是littleBtn是在cell中,所以可以用indexPath來記錄自身位置.

我們知道tableView的cell一開始并不會(huì)都創(chuàng)建出來(緩存池機(jī)制),那么直接在cell里添加這兩條具有記錄功能的屬性勢必會(huì)因?yàn)閺?fù)用機(jī)制導(dǎo)致一些數(shù)據(jù)錯(cuò)誤問題,關(guān)鍵是要實(shí)現(xiàn)數(shù)據(jù)和UI的分離, 主要思路是: 每次cell出現(xiàn)時(shí)都必須判斷一下cell的是否點(diǎn)擊狀態(tài) . 那么這里就很明顯了,應(yīng)該將這兩條具有記錄功能的屬性放在model里.

以下是本人在項(xiàng)目中的代碼

相關(guān)tableView
//
//  CompetitionSelView.h
//  Created by 鄧昊 on 2017/9/6.
//  Copyright ? 2017年 鄧昊. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CompetitionLeaguesModel.h"

@class CompetitionSelCell;

@interface CompetitionSelView : UIView <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *allMatch_btn;
@property (nonatomic, strong) UIButton *complete_btn;

@property (nonatomic, assign) BOOL allMatch_btnHasClicked;//allBtn是否被點(diǎn)擊

@property (nonatomic, strong) NSMutableArray <CompetitionLeaguesModel *> *leaguesModels; //數(shù)據(jù)model

@end



//
//  CompetitionSelView.m
//  Created by 鄧昊 on 2017/9/6.
//  Copyright ? 2017年 鄧昊. All rights reserved.
//

#import "CompetitionSelView.h"
#import "CompetitionSelCell.h"

@implementation CompetitionSelView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.allMatch_btnHasClicked = YES;
    
 ...布局相關(guān)這里省略...
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.leaguesModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CompetitionSelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.leaguesModel = self.leaguesModels[indexPath.row];//
    cell.leaguesModel.indexPath = indexPath;  //關(guān)鍵: 將indexPath記錄進(jìn)model
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.allMatch_btnHasClicked == YES) {
        [cell.sel_btn setBackgroundImage:[UIImage imageNamed:@"inform_select"] forState:UIControlStateNormal];
        cell.leaguesModel.isSel = YES;
    }else{

        if (cell.leaguesModel.isSel == YES ) {
            [cell.sel_btn setBackgroundImage:[UIImage imageNamed:@"inform_select"] forState:UIControlStateNormal];
        }else{
            [cell.sel_btn setBackgroundImage:[UIImage imageNamed:@"inform_normal"] forState:UIControlStateNormal];
        }
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CompetitionSelCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.leaguesModel.isSel == NO ) {
        [cell.sel_btn setBackgroundImage:[UIImage imageNamed:@"inform_select"] forState:UIControlStateNormal];
        cell.leaguesModel.isSel = YES;
    }else{
        [cell.sel_btn setBackgroundImage:[UIImage imageNamed:@"inform_normal"] forState:UIControlStateNormal];
        cell.leaguesModel.isSel = NO;
        
    }

    //關(guān)鍵:  每次點(diǎn)擊完littleBtn后都要進(jìn)行一次判斷 這里思路是:遍歷model,如果任意一個(gè)model.isSel == NO, 那么取消allBtn的全選狀態(tài)
    
    BOOL isAllSelected = YES;
    for (CompetitionLeaguesModel *model in self.leaguesModels) {
        BOOL isSel = model.isSel;
        
        if (isSel == NO) {  //只要是一個(gè)為no 就不選中
            isAllSelected = NO;
        }
        
    }
    
    [self allSelectBtnStatus:isAllSelected];
    
}

#pragma mark - 每次小 btn 點(diǎn)擊后 都需判斷一下 allBtn狀態(tài)
- (void)allSelectBtnStatus:(BOOL)allSelected{

    if (allSelected == YES) {
        [self.allMatch_btn setBackgroundImage:[UIImage imageNamed:@"inform_select"] forState:UIControlStateNormal];
        self.allMatch_btnHasClicked = YES;
    }else{
        [self.allMatch_btn setBackgroundImage:[UIImage imageNamed:@"inform_normal"] forState:UIControlStateNormal];
        self.allMatch_btnHasClicked = NO;
    }

}

#pragma mark - allBtn click
- (void)allSelBtnClick {
    self.allMatch_btnHasClicked = !self.allMatch_btnHasClicked;

    if (self.allMatch_btnHasClicked == YES) {  //全選中
        
        [self.allMatch_btn setBackgroundImage:[UIImage imageNamed:@"inform_select"] forState:UIControlStateNormal];

        for (CompetitionLeaguesModel *model in self.leaguesModels) {
            model.isSel = YES;
        }
        
    }else{   //全取消
        
        [self.allMatch_btn setBackgroundImage:[UIImage imageNamed:@"inform_normal"] forState:UIControlStateNormal];

        
        for (CompetitionLeaguesModel *model in self.leaguesModels) {
            model.isSel = NO;
        }
        
    }
    
    [self.tableView reloadData];
 
}

@end


相關(guān)model
//
//  CompetitionLeaguesModel.h
//  Created by 鄧昊 on 2017/9/7.
//  Copyright ? 2017年 鄧昊. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CompetitionLeaguesModel : NSObject

@property (nonatomic, assign) NSInteger id;
@property (nonatomic, copy) NSString *name;

@property (nonatomic, assign) BOOL isSel;   //cell是否被勾選上
@property (nonatomic, strong) NSIndexPath *indexPath;   //cell在tableView的位置信息

+(NSMutableArray *)CompetitionLeaguesModelWithResponse:(XYApiResponse *)response;

@end

總結(jié)

關(guān)鍵兩點(diǎn):
1.將cell的 indexPath記錄進(jìn)所屬model
2.每次點(diǎn)擊完littleBtn后都要進(jìn)行一次判斷 我的方法是遍歷model,如果任意一個(gè)model.isSel == NO, 那么取消allBtn的全選狀態(tài)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末壁晒,一起剝皮案震驚了整個(gè)濱河市减宣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖友扰,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泌霍,死亡現(xiàn)場離奇詭異梆靖,居然都是意外死亡嵌纲,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門褥伴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谅将,“玉大人,你說我怎么就攤上這事重慢〖⒈郏” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵似踱,是天一觀的道長隅熙。 經(jīng)常有香客問我,道長核芽,這世上最難降的妖魔是什么囚戚? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮轧简,結(jié)果婚禮上驰坊,老公的妹妹穿的比我還像新娘。我一直安慰自己哮独,他們只是感情好庐橙,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布假勿。 她就那樣靜靜地躺著,像睡著了一般态鳖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恶导,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天浆竭,我揣著相機(jī)與錄音,去河邊找鬼惨寿。 笑死邦泄,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的裂垦。 我是一名探鬼主播顺囊,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蕉拢!你這毒婦竟也來了特碳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤晕换,失蹤者是張志新(化名)和其女友劉穎午乓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體闸准,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡益愈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夷家。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒸其。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖库快,靈堂內(nèi)的尸體忽然破棺而出摸袁,到底是詐尸還是另有隱情,我是刑警寧澤缺谴,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布但惶,位于F島的核電站,受9級(jí)特大地震影響湿蛔,放射性物質(zhì)發(fā)生泄漏膀曾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一阳啥、第九天 我趴在偏房一處隱蔽的房頂上張望添谊。 院中可真熱鬧,春花似錦察迟、人聲如沸斩狱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽所踊。三九已至泌枪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秕岛,已是汗流浹背碌燕。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留继薛,地道東北人修壕。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像遏考,于是被迫代替她去往敵國和親慈鸠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 2017.02.22 可以練習(xí)灌具,每當(dāng)這個(gè)時(shí)候青团,腦袋就犯困,我這腦袋真是神奇呀稽亏,一說讓你做事情壶冒,你就犯困,你可不要太...
    Carden閱讀 1,342評(píng)論 0 1
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件截歉,我們平時(shí)使用的軟件中到處都可以看到它的影子胖腾,類似...
    liudhkk閱讀 9,038評(píng)論 3 38
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改變導(dǎo)航欄背景顏...
    SadMine閱讀 1,576評(píng)論 1 4
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,662評(píng)論 1 9
  • 9:31分,孩子終于睡著了瘪松。 我松了一口氣咸作,8點(diǎn)鐘,去給老公盛飯熱菜宵睦,小家伙自己在房間不知道怎么就急了记罚。爸爸過來,...
    寶媽昭嫻閱讀 165評(píng)論 0 0