TableView Section展开与收缩

//
//  MainVC.m
//  Test
//
//  Created by mccRee on 2018/8/8.
//  Copyright © 2018年 mccRee. All rights reserved.
//

#import "MainVC.h"

@interface MainVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_allArray;//创建一个数据源数组
    NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开
}
@property (nonatomic,strong)UITableView *tableView;

@end

@implementation MainVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    dic = [NSMutableDictionary dictionary];
    _allArray = [@[@[@"12",@"32",@"12",@"12",@"12",@"32"],@[@"12",@"12",@"12",@"32",@"12",@"12",@"12",@"32",@"12",@"12",@"12",@"32"],@[@"12",@"12",@"32"]]mutableCopy];
    [self.view addSubview:self.tableView];
}

//懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 375, 667 - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allArray.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

//

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor redColor];
    //创建一个手势进行点击,这里可以换成button
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_tap:)];
    view.tag = 300 + section;
    [view addGestureRecognizer:tap];
    return view;
}

- (void)action_tap:(UIGestureRecognizer *)tap{
    NSString *str = [NSString stringWithFormat:@"%ld",tap.view.tag - 300];
    if ([dic[str] integerValue] == 0) {//如果是0,就把1赋给字典,打开cell
        [dic setObject:@"1" forKey:str];
    }else{//反之关闭cell
        [dic setObject:@"0" forKey:str];
    }
    // [self.tableView reloadData];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[str integerValue]] withRowAnimation:UITableViewRowAnimationFade];//有动画的刷新
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *string = [NSString stringWithFormat:@"%ld",section];
    if ([dic[string] integerValue] == 1 ) {  //打开cell返回数组的count
        NSArray *array = [NSArray arrayWithArray:_allArray[section]];
        return array.count;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = [UIColor orangeColor];
    cell.textLabel.text = _allArray[indexPath.section][indexPath.row];
    return cell;
}

@end

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这个小demo是之前项目中使用的一个需求,单独拿出来,效果还不错。主要是利用tableView自带刷新效果和scr...
    天明天阅读 10,178评论 0 4
  • 关于TableViewCell的收缩展开与动态高度的方法网上有很多,但本文主要运用AutoLayout和更改Con...
    4d1487047cf6阅读 11,016评论 6 20
  • 今日书籍:《内向者沟通圣经》 我们的脑海里植入了这样的刻板印象:内向就是害羞敏感,沉默寡言,黯淡无光。外向就是积极...
    麦小麦的小岛阅读 1,274评论 0 4
  • 换季 一场秋雨一场寒。天冷了,不得不翻箱找衣服。顺手就扒拉出来那套灰色的秋衣秋裤,顺带揪出来那件暗红色的毛衣。看了...
    1素心阅读 1,693评论 1 0
  • 每年的年夜饭,吃的不是精心准备的菜肴,而是家的味道,是一家人其乐融融地坐在一起吃的团圆饭。今年新学了两道创新莱,五...
    青青wq阅读 3,077评论 0 1

友情链接更多精彩内容