#import <UIKit/UIKit.h>ide
@interface FirstViewController : UIViewController動畫
@endatom
#import "FirstViewController.h"spa
#import "TapCellViewController.h"orm
@interface FirstViewController () <UITableViewDataSource,UITableViewDelegate>get
{string
UITableView *_tableView;it
}io
@property(nonatomic,retain)NSMutableArray *arrayDatas;table
@end
@implementation FirstViewController
- (NSMutableArray *)arrayDatas
{
if (_arrayDatas == nil) {
_arrayDatas = [NSMutableArray array];
for (int i = 'A'; i <= 'C'; i++) {
//字典中保存的數據 1> 要顯示的原始數據 2> 當前分組是否展開
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSMutableArray *sub = [NSMutableArray array];
for (int j = 0; j< 5; j++) {//擴展的元素數目
NSString *str = [NSString stringWithFormat:@"%c%d",i,j];
[sub addObject:str];
}
[dict setObject:sub forKey:@"KEY_ARRAY"];
[dict setObject:[NSNumber numberWithBool:NO] forKey:@"KEY_STATE"];//默認爲展開狀態 YES展開 NO不展開
[_arrayDatas addObject:dict];
}
}
return _arrayDatas;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"好友列表";
self.view.backgroundColor = [UIColor redColor];
[self createTableView];
// Do any additional setup after loading the view.
}
- (void)createTableView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
_tableView = tableView;
[self.view addSubview:_tableView];
}
#pragma mark --協議
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arrayDatas count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// if (section == 1) {
// return 0;
// }
NSDictionary *dict = [self.arrayDatas objectAtIndex:section];
BOOL isExpand = [[dict objectForKey:@"KEY_STATE"] boolValue];
if (!isExpand) {
return 0;//不展開,返回的是0,就不須要顯示了
}
//展開返回實際行數
return [[dict objectForKey:@"KEY_ARRAY"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
NSDictionary *dict = [self.arrayDatas objectAtIndex:indexPath.section];
NSArray *array = [dict objectForKey:@"KEY_ARRAY"];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
//設置一下分組頭部高度,就能顯示了
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;//設置0 就會是默認值 不變的
}
//分組的cellHeaderView上返回一個button 根據button.tag關閉展開的cell section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
NSString *str = [NSString stringWithFormat:@"第%ld組",section];
[btn setTitle:str forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:26];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//設置按鈕的tag值
btn.tag = section + 100;
return btn;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TapCellViewController *tap = [[TapCellViewController alloc]init];
[self.navigationController pushViewController:tap animated:YES];
}
#pragma mark --分段頭部按鈕點擊調用
- (void)btnClick:(UIButton *)btn
{
//取到數據 修改狀態 從新加載 ---核心思想
NSInteger section = btn.tag - 100;
NSMutableDictionary *dict = [self.arrayDatas objectAtIndex:section];
//修改展開狀態
BOOL isExpand = [[dict objectForKey:@"KEY_STATE"] boolValue];
//改爲原來相反的狀態
[dict setObject:[NSNumber numberWithBool:!isExpand]forKey:@"KEY_STATE"];
[_tableView reloadData]; //這種刷新太突兀了
//刷新 用動畫形式
// NSIndexSet *set = [[NSIndexSet alloc] initWithIndex:section];
// [_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationBottom];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end