表格分组折叠

初始化数据

- (void)initData {


    _mArrayData = [[NSMutableArray alloc]init];

    

    for (int i = 'A'; i <= 'Z'; i++) {

        NSMutableDictionary *smallGroupDict = [[NSMutableDictionary alloc]init];  //声明小数组的字典

        [smallGroupDict setObject:[NSString stringWithFormat:@"%c",i] forKey:GROUP_NAME];

        [smallGroupDict setObject:[NSNumber numberWithBool:YES] forKey:GROUP_STATE]; //该组的状态是收起的

        

        NSMutableArray *subArray = [[NSMutableArray alloc]init];

        for (int j = 0; j < 5; j++) {

            NSString *str = [NSString stringWithFormat:@"%c%d",i,j];

            [subArray addObject:str];

        }

        

        [smallGroupDict setObject:subArray forKey:GROUP_CONTENT];

        [_mArrayData addObject:smallGroupDict];

    }

}


然后设置section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    return _mArrayData.count;

}

#pragma mark - 获取每一组的行数,必须实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    NSMutableDictionary *smallDict = [_mArrayData objectAtIndex:section];

    //section 获得当前组数

    NSNumber *number = [smallDict objectForKey:GROUP_STATE];

    if ([number boolValue]) {

        return 0;

    }

    

    NSArray *array = [smallDict objectForKey:GROUP_CONTENT];

    return array.count; //返回当前组数的行数

}

#pragma mark - 获取单元格对象,必须实现的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //indexPath 代表的是获得的当前组数的第几行

    

    NSString *str = @"ID";

    

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; //获取可以重复利用的单元格

        

    if (cell == nil) {  //没有获得可以重复利用的单元格

        

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];

    }

    

    NSDictionary *smallGroupDict = [_mArrayData objectAtIndex:indexPath.section]; //获取当前对应的小组对象

    NSArray *smallGroup = [smallGroupDict objectForKey:GROUP_CONTENT];

    NSString *strValue = [smallGroup objectAtIndex:indexPath.row];  //获取对应行的数据

    

    cell.textLabel.text = strValue;  //单元格文字赋值

    

    return cell;

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {


    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button setTitle:[NSString stringWithFormat:@"%c",section] forState:UIControlStateNormal];

    button.frame = CGRectMake(0, 0, 320, 30);

    button.tag = 100+section;

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    

    if (section % 2 == 0) {

        button.backgroundColor = [UIColor blueColor];

    }

    else {

    

        button.backgroundColor = [UIColor greenColor];

    }

    

    self.myBlock = ^(id sender){

        NSMutableDictionary *dict = [_mArrayData objectAtIndex:section];

        NSNumber *number = [dict objectForKey:GROUP_STATE];

        if ([number boolValue]) {

            [dict setObject:[NSNumber numberWithBool:NO] forKey:GROUP_STATE];

        }

        else {

            

            [dict setObject:[NSNumber numberWithBool:YES] forKey:GROUP_STATE];

        }

        

        [_tabelView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];


    };


    return button;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {


    return 30.0f;

}


- (void)buttonClick:(UIButton *)btn {

    

    if (self.myBlock) {

        self.myBlock (btn.tag);

    }


//    NSMutableDictionary *dict = [_mArrayData objectAtIndex:btn.tag-100];

//    NSNumber *number = [dict objectForKey:GROUP_STATE];

//    if ([number boolValue]) {

//        [dict setObject:[NSNumber numberWithBool:NO] forKey:GROUP_STATE];

//    }

//    else {

//    

//        [dict setObject:[NSNumber numberWithBool:YES] forKey:GROUP_STATE];

//    }

    

    [_tabelView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag-100] withRowAnimation:UITableViewRowAnimationTop];

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值