好友分組的點(diǎn)開(kāi)與點(diǎn)關(guān)

效果:(界面有點(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末普碎,一起剝皮案震驚了整個(gè)濱河市吼肥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌麻车,老刑警劉巖缀皱,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異动猬,居然都是意外死亡啤斗,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)赁咙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)钮莲,“玉大人,你說(shuō)我怎么就攤上這事彼水〈抻担” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵凤覆,是天一觀(guān)的道長(zhǎng)链瓦。 經(jīng)常有香客問(wèn)我,道長(zhǎng)盯桦,這世上最難降的妖魔是什么慈俯? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮拥峦,結(jié)果婚禮上贴膘,老公的妹妹穿的比我還像新娘。我一直安慰自己略号,他們只是感情好刑峡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著玄柠,像睡著了一般氛琢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上随闪,一...
    開(kāi)封第一講書(shū)人閱讀 51,692評(píng)論 1 305
  • 那天阳似,我揣著相機(jī)與錄音,去河邊找鬼铐伴。 笑死撮奏,一個(gè)胖子當(dāng)著我的面吹牛俏讹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播畜吊,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼泽疆,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了玲献?” 一聲冷哼從身側(cè)響起殉疼,我...
    開(kāi)封第一講書(shū)人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎捌年,沒(méi)想到半個(gè)月后瓢娜,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡礼预,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年眠砾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片托酸。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡褒颈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出励堡,到底是詐尸還是另有隱情谷丸,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布应结,位于F島的核電站刨疼,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏摊趾。R本人自食惡果不足惜币狠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一游两、第九天 我趴在偏房一處隱蔽的房頂上張望砾层。 院中可真熱鬧,春花似錦贱案、人聲如沸肛炮。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)侨糟。三九已至,卻和暖如春瘩燥,著一層夾襖步出監(jiān)牢的瞬間秕重,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工厉膀, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留溶耘,地道東北人二拐。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像凳兵,于是被迫代替她去往敵國(guó)和親百新。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355