
借問漢宮誰得似,可憐飛燕倚新妝!<角金魚>
這個方法不很高明但是總結(jié)一下,加強(qiáng)自己對控件的掌握吧!以后有更好的方法,再來總結(jié).

首先看一下效果.gif
- 在TableViewController里面實(shí)現(xiàn)
定義兩個屬性:
// 數(shù)據(jù)源數(shù)組 用來存儲要展示的數(shù)據(jù)
@property (strong, nonatomic) NSMutableArray *dataArray;
// 記錄收縮狀態(tài) 對應(yīng)的把每個區(qū)的展開收縮記錄下來
@property (strong, nonatomic) NSMutableDictionary *dataDic;
這里給一個展示數(shù)據(jù):
// 用一組假的數(shù)據(jù)放到 tableview上面
self.dataArray = [@[@[@"詹姆斯",@"韋德",@"安東尼"],@[@"趙四",@"謝廣坤",@"劉能"],@[@"騷軍",@"蓬勃",@"成龍大哥"],@[@"小三",@"小四",@"小五"]] mutableCopy];
// 初始化用于標(biāo)記某一個分區(qū)的section的折疊狀態(tài)的字典
self.dataDic = [NSMutableDictionary dictionary];
//self.dataDic = [@{@"0":@"1",@"1":@"1",@"2":@"1",@"3":@"1"} mutableCopy];
// 這里可以給對應(yīng)的區(qū)一個初始狀態(tài),也可以不設(shè)置,因?yàn)闀邳c(diǎn)擊section的方法中進(jìn)行賦值
// 返回分區(qū)數(shù)
```code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}```
// 判斷到底是正常顯示row還是row不顯示(返回0行) 這里是 等于 0(字符串類型)的時候 展開
```code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取出字典中的 section 如果是第 0 個分區(qū) 那么就返回該分區(qū)的數(shù)據(jù)
if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",section]] isEqualToString:@"0"])
{
NSLog(@"----------------");
return [self.dataArray[section] count];
}else
{
NSLog(@"**************");
return 0;
}
}```
// 設(shè)置cell上顯示的內(nèi)容
```code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:(arc4random()%173)/346.0 + 0.5 green:(arc4random()%173)/346.0 + 0.5 blue:(arc4random()%173)/346.0 + 0.5 alpha: 1];
cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];
return cell;
}```
// 在返回頭視圖的方法里面給每個區(qū)添加一個button
```code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 把分區(qū)的頭視圖設(shè)置成Button
UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
button.backgroundColor = [UIColor redColor];
// 設(shè)置Button的標(biāo)題作為section的標(biāo)題用
[button setTitle:[NSString stringWithFormat:@"第 %ld 組",section] forState:(UIControlStateNormal)];
// 設(shè)置點(diǎn)擊事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchDown)];
// 給定tag值用于確定點(diǎn)擊的對象是哪個區(qū)
button.tag = section + 1000;
return button;
}```
// 設(shè)置Button的點(diǎn)擊事件
```code
- (void)buttonAction:(UIButton *)sender
{
NSInteger temp = sender.tag - 1000;
// 修改 每個區(qū)的收縮狀態(tài) 因?yàn)槊看吸c(diǎn)擊后對應(yīng)的狀態(tài)改變 temp代表是哪個section
if ([[self.dataDic valueForKey:[NSString stringWithFormat:@"%ld",temp]]isEqualToString:@"0"] )
{
[self.dataDic setObject:@"1" forKey:[NSString stringWithFormat:@"%ld",temp]];
}else
{
[self.dataDic setObject:@"0" forKey:[NSString stringWithFormat:@"%ld",temp]];
}
// 更新 section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:temp] withRowAnimation:(UITableViewRowAnimationFade)];
}```