关于TableView的点击收起和展开

本文是一个关于简单的完成TableView段头点击能展开和收起功能的Demo

ViewController.m中如下:

#import "ViewController.h"

#define UIScreen_Width ([UIScreen mainScreen].bounds.size.width)
#define UIScreen_height ([UIScreen mainScreen].bounds.size.height)

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView* _tableView;
    NSMutableArray *openArray;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    openArray = [[NSMutableArray alloc] init];
    NSNumber* num = [NSNumber numberWithBool:YES];
    //有几个section就要存几个openArray,在项目中就要注意这个openArray是根据后台数据获得的
    [openArray addObject:num];
    [openArray addObject:num];

    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, UIScreen_Width, UIScreen_height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([[openArray objectAtIndex:section] boolValue]  == NO) {
        return 0;
    }
    return 10;
}

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

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

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(0, 0, UIScreen_Width, 35);
    button.tag = section;
    [button setBackgroundColor:[UIColor blueColor]];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (void)buttonClick:(UIButton*)button{
    BOOL isOpen = [[openArray objectAtIndex:button.tag] boolValue];
    if (isOpen == YES) {
        NSNumber* num = [NSNumber numberWithBool:NO];
        [openArray replaceObjectAtIndex:button.tag withObject:num];
    } else {
        NSNumber* num = [NSNumber numberWithBool:YES];
        [openArray replaceObjectAtIndex:button.tag withObject:num];
    }
    
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,751评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,262评论 4 61
  • 古人云:知之者不如好之者,好之者不如乐之者,乐之者不如志之者。上天给我了我们限的时间、有限的天赋,却留给我们无数的...
    一笑而过2023阅读 233评论 0 1
  • 016-12-15 一月我行走在烟雨江南 杏花雨湿了我的衣衫 但杨柳风 吹面不寒 二月我煮酒忆长安 踏青时柳叶青眼...
    Lotus吴阅读 306评论 0 2
  • 说不出是什么时候开始害怕夜晚的,尤其是冬天的夜晚,感觉特别的清冷,加上对黑的恐惧觉得那种夜晚特别的刺骨。真的记不清...
    滕熙阅读 354评论 0 0