tableView展开和收起

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

#import "FirstViewController.h"

#import "TapCellViewController.h"

@interface FirstViewController () <UITableViewDataSource,UITableViewDelegate>

{

    UITableView *_tableView;

}

@property(nonatomic,retain)NSMutableArray *arrayDatas;

@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

 

 

 

转载于:https://my.oschina.net/u/2319073/blog/675745

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值