效果:(界面有點(diǎn)丑让禀,請(qǐng)別介意)
方式1:字典存狀態(tài),通用所有
#import "XHGPSDemoViewController.h"@interface XHGPSDemoViewController ()/** tableView*/
@property (nonatomic, strong) UITableView * tableView;
/** 數(shù)據(jù)數(shù)組*/
@property (nonatomic, strong) NSMutableArray * dataArray;
/** 記錄組的開(kāi)關(guān)*/
@property (nonatomic, strong) NSMutableDictionary * sectionStateDic;
@end
@implementation XHGPSDemoViewController
#pragma mark - LazyLoad 懶加載
- (UITableView *)tableView {
? ? if (_tableView == nil) {
? ? ? ? _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
? ? ? ? _tableView.delegate = self;
? ? ? ? _tableView.dataSource = self;
? ? ? ? _tableView.tableFooterView = [UIView new];
? ? ? ? [self.view addSubview:_tableView];
? ? }
? ? return _tableView;
}
- (NSMutableArray *)dataArray {
? ? if (_dataArray == nil) {
? ? ? ? _dataArray = [NSMutableArray new];
? ? }
? ? return _dataArray;
}
- (NSMutableDictionary *)sectionStateDic {
? ? if (_sectionStateDic == nil) {
? ? ? ? _sectionStateDic = [NSMutableDictionary new];
? ? }
? ? return _sectionStateDic;
}
#pragma mark - System Method 系統(tǒng)方法
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? [self configSubViews];
? ? [self transData];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
#pragma mark - Custom Method 自定義方法
/** 配置子視圖陨界、子控件 */
- (void)configSubViews {
? ? UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
? ? self.tableView.tableHeaderView = headerView;
}
#pragma mark - TableView DataSource 數(shù)據(jù)源方法(TableVieW)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? NSArray *listArray = self.dataArray[section];
? ? BOOL isHiden = [[self.sectionStateDic objectForKey:@(section)] boolValue];
? ? if (isHiden) {
? ? ? ? return 0;
? ? }
? ? return listArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
? ? return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? static NSString *cellId = @"CellId";
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
? ? if (cell == nil) {
? ? ? ? cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
? ? }
? ? NSArray *listArray = self.dataArray[indexPath.section];
? ? cell.textLabel.text = listArray[indexPath.row];
? ? return cell;
}
#pragma mark - TableView Delegate 代理(TableVieW)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
? ? return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
? ? UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
? ? [sectionBtn setTitle:@"點(diǎn)下就關(guān)了" forState:UIControlStateNormal];
? ? [sectionBtn setTitle:@"點(diǎn)下就開(kāi)了" forState:UIControlStateSelected];
? ? [sectionBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
? ? [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
? ? sectionBtn.backgroundColor = [UIColor cyanColor];
? ? [sectionBtn addTarget:self action:@selector(sectionBtnAction:) forControlEvents:UIControlEventTouchUpInside];
? ? /** 設(shè)置狀態(tài)*/
? ? sectionBtn.tag = section + 1000;
? ? sectionBtn.selected = [[self.sectionStateDic objectForKey:@(section)] boolValue];
? ? return sectionBtn;
}
#pragma mark - NetWork 網(wǎng)絡(luò)請(qǐng)求
/** 請(qǐng)求數(shù)據(jù) */
- (void)transData {
? ? /** 數(shù)據(jù)數(shù)組*/
? ? NSArray *firstSectionArray = @[@"00",@"01",@"02",@"03",@"04",@"05",@"06"];
? ? NSArray *secSectionArray = @[@"10",@"11",@"12",@"13",@"14"];
? ? NSArray *thirdSectionArray = @[@"20",@"21",@"22",@"23",@"24",@"25"];
? ? /** 添加數(shù)據(jù)*/
? ? [self.dataArray addObject:firstSectionArray];
? ? [self.dataArray addObject:secSectionArray];
? ? [self.dataArray addObject:thirdSectionArray];
? ? /** 狀態(tài)*/
? ? for (int i = 0; i < 3; i ++) {
? ? ? ? [self.sectionStateDic setObject:@(YES) forKey:@(i)];
? ? }
? ? [self.tableView reloadData];
}
#pragma mark - Action 響應(yīng)事件
- (void)sectionBtnAction:(UIButton *)btn {
? ? btn.selected = !btn.selected;
? ? NSInteger section = btn.tag - 1000;
? ? [self.sectionStateDic setObject:@(btn.selected) forKey:@(section)];
? ? [self.tableView reloadData];
}
@end
方式2:模型存狀態(tài)巡揍,通用模型
#import@class List;@interface XHGPSDemoModel : NSObject/** 類(lèi)型*/@property (nonatomic, copy) NSString * type;/** 分類(lèi)名字*/@property (nonatomic, copy) NSString * classifyName;/** 模型數(shù)組*/@property (nonatomic, copy) NSArray *list;
/** 是否關(guān)*/
@property (nonatomic, assign) BOOL isClose;
@end
@interface List : NSObject
/** 名字*/
@property (nonatomic, copy) NSString * name;
/** 圖片地址*/
@property (nonatomic, copy) NSString * imgUrl;
@end
#import "XHGPSDemoViewController.h"#import "XHGPSDemoModel.h"@interface XHGPSDemoViewController ()/** tableView*/
@property (nonatomic, strong) UITableView * tableView;
/** 數(shù)據(jù)數(shù)組*/
@property (nonatomic, strong) NSMutableArray * dataArray;
@end
@implementation XHGPSDemoViewController
#pragma mark - LazyLoad 懶加載
- (UITableView *)tableView {
? ? if (_tableView == nil) {
? ? ? ? _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
? ? ? ? _tableView.delegate = self;
? ? ? ? _tableView.dataSource = self;
? ? ? ? _tableView.tableFooterView = [UIView new];
? ? ? ? [self.view addSubview:_tableView];
? ? }
? ? return _tableView;
}
- (NSMutableArray *)dataArray {
? ? if (_dataArray == nil) {
? ? ? ? _dataArray = [NSMutableArray new];
? ? }
? ? return _dataArray;
}
#pragma mark - System Method 系統(tǒng)方法
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.view.backgroundColor = [UIColor whiteColor];
? ? [self configSubViews];
? ? [self transData];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
#pragma mark - Custom Method 自定義方法
/** 配置子視圖、子控件 */
- (void)configSubViews {
? ? UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
? ? self.tableView.tableHeaderView = headerView;
}
#pragma mark - TableView DataSource 數(shù)據(jù)源方法(TableVieW)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? XHGPSDemoModel *model = self.dataArray[section];
? ? BOOL isHiden = model.isClose;
? ? if (isHiden) {
? ? ? ? return 0;
? ? }
? ? return model.list.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
? ? return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? static NSString *cellId = @"CellId";
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
? ? if (cell == nil) {
? ? ? ? cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
? ? }
? ? XHGPSDemoModel *model = self.dataArray[indexPath.section];
? ? List *listModel = model.list[indexPath.row];
? ? cell.textLabel.text = listModel.name;
? ? return cell;
}
#pragma mark - TableView Delegate 代理(TableVieW)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
? ? return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
? ? UIButton *sectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
? ? [sectionBtn setTitle:@"點(diǎn)下就關(guān)了" forState:UIControlStateNormal];
? ? [sectionBtn setTitle:@"點(diǎn)下就開(kāi)了" forState:UIControlStateSelected];
? ? [sectionBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
? ? [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
? ? sectionBtn.backgroundColor = [UIColor cyanColor];
? ? [sectionBtn addTarget:self action:@selector(sectionBtnAction:) forControlEvents:UIControlEventTouchUpInside];
? ? /** 設(shè)置狀態(tài)*/
? ? sectionBtn.tag = section + 1000;
? ? XHGPSDemoModel *model = self.dataArray[section];
? ? sectionBtn.selected = model.isClose;
? ? return sectionBtn;
}
#pragma mark - NetWork 網(wǎng)絡(luò)請(qǐng)求
/** 請(qǐng)求數(shù)據(jù) */
- (void)transData {
? ? /** 狀態(tài) & 數(shù)據(jù)*/
? ? for (int i = 0; i < 3; i ++) {
? ? ? ? XHGPSDemoModel *model = [[XHGPSDemoModel alloc] init];
? ? ? ? NSMutableArray *listArray = [NSMutableArray new];
? ? ? ? for (int j = 0; j < 5; j ++) {
? ? ? ? ? ? List *listModel = [[List alloc] init];
? ? ? ? ? ? listModel.name = [NSString stringWithFormat:@"第%ld組 ,第%ld個(gè)",i,j];
? ? ? ? ? ? [listArray addObject:listModel];
? ? ? ? }
? ? ? ? /** 默認(rèn)關(guān)*/
? ? ? ? model.isClose = YES;
? ? ? ? model.list = listArray;
? ? ? ? [self.dataArray addObject:model];
? ? }
? ? [self.tableView reloadData];
}
#pragma mark - Action 響應(yīng)事件
- (void)sectionBtnAction:(UIButton *)btn {
? ? btn.selected = !btn.selected;
? ? NSInteger section = btn.tag - 1000;
? ? XHGPSDemoModel *model = self.dataArray[section];
? ? model.isClose = btn.selected;
? ? [self.tableView reloadData];
}
@end