TableView类似QQ分组的折叠与展开

类似QQ分组的样子,实现tableView的折叠与展开。其实要做这个效果我先想到的是在tableView中再嵌套多个tableView,这个想法实现起来就有点难了。
所以还是换个思路,把tableView的HeaderView用上了。给headerView加上手势,轻松解决折叠展开的问题。
直接上代码吧。

@property (nonatomic, strong) UITableView *myTableView;  
@property (nonatomic, strong) NSMutableArray *listArray;        // 数据源
@property (nonatomic, strong) NSMutableArray *titlesArray;     // 分组的名称
@property (nonatomic, strong) NSMutableDictionary *openSectionDict; // 记录哪个组展开
- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化tableView
    _myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    
    self.openSectionDict = [[NSMutableDictionary alloc] init]; // 初始化字典
    [self setUpData];
}
// 给数据源赋值
- (void)setUpData {
    self.listArray = [NSMutableArray new];
    self.titlesArray = [NSMutableArray new];
    for (int i = 0; i < 5; i++) {     // 5个section
        [self.titlesArray addObject:[NSString stringWithFormat:@"section %d", i]];
        NSMutableArray *array = [NSMutableArray new];
        for (int i = 0; i < 4; i++) {   // 每个section有4个row
            [array addObject:[NSString stringWithFormat:@"row %d", i]];
        }
        [self.listArray addObject:array];
    }
}
// 实现tableView的代理方法
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) {    //根据记录的展开状态设置row的数量
        return 0;
    } else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL_ID"];
        cell.textLabel.text = [NSString stringWithFormat:@"row %ld", indexPath.row];
    }
    return cell;
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
    view.backgroundColor = [UIColor whiteColor];
    view.tag = KTAG + section;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, view.bounds.size.width, view.bounds.size.height)];
    label.text = self.titlesArray[section];
    [view addSubview:label];
    
    if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 10) / 2, 7, 10)];
        imageView.image = [UIImage imageNamed:@"Triangle_right_gray"];   //  三角形小图片
        [view addSubview:imageView];
    } else {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 7) / 2, 10, 7)];
        imageView.image = [UIImage imageNamed:@"Triangle_down_gray"];
        [view addSubview:imageView];
    }
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collegeTaped:)];
    [view addGestureRecognizer:tap];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.1;
}

#pragma mark - sectionHeader clicked
- (void)collegeTaped:(UITapGestureRecognizer *)sender {
    NSString *key = [NSString stringWithFormat:@"%ld", sender.view.tag - KTAG];
    //   给展开标识赋值
    if ([[self.openSectionDict objectForKey:key] integerValue] == 0) {
        [self.openSectionDict setObject:@"1" forKey:key];
    } else {
        [self.openSectionDict setObject:@"0" forKey:key];
    }
    NSUInteger index = sender.view.tag;
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:index - KTAG];
    [self.myTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

最后的效果:

Simulator Screen Shot 2016年11月14日 下午2.46.25.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,871评论 22 665
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 在人生的道路中,我们会遇到不同的选择,很多东西都会让我们难于取舍,
    旭轨迹阅读 178评论 0 0
  • 巧丫原名春子,巧丫是我对她的爱称,因为她心灵手巧,聪明能干,所以我称她巧丫。巧丫比我大一岁,我们初中同学仅一年,却...
    若水_086阅读 962评论 21 25
  • 01 那天孩子们放学后,我像往常一样接了他们,每天孩子们在小区和一些同龄的孩子玩耍。也像平时一样我只在远远的地方看...
    喻心阅读 400评论 0 8