#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