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

来源:互联网 发布:nginx lua 编辑:程序博客网 时间:2024/05/17 03:56

类似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];}

最后的效果:



文/汪小喵(简书作者)
原文链接:http://www.jianshu.com/p/04777439451f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 铁锅炒菜菜发黑怎么办 红豆沙馅太稀了怎么办 买的豆沙馅太稀怎么办 做的豆馅稀了怎么办 包豆沙包馅稀了怎么办 软件自定义权限重名怎么办 窗户外有垃圾桶怎么办 菜地里有蚂蚁怎么办 灯光吸引的飞虫怎么办 广告机格式化了怎么办 proe约束冲突了怎么办 中午考试想睡觉怎么办 喝了红茶睡不着怎么办 考试前状态不好怎么办 考前紧张睡不着觉怎么办 通宵失眠第二天怎么办 考试前睡不着觉怎么办 一到晚上睡不着怎么办 明天考试听力差怎么办 天天晚上睡不着觉怎么办 因紧张睡不着觉怎么办 房卡找不到了怎么办 马代不会英文怎么办 剩下的蒸米饭怎么办 临时牌照违法了怎么办 打12345不管用怎么办 省政府改变了中央文件怎么办 应聘时学历不够怎么办 做导游学历不够怎么办 市长热线打不通怎么办 12315网站不受理怎么办 法律文书生效前转后财产怎么办 打12345投诉没用怎么办 单位医保停了怎么办 iphonex开不了机怎么办 会计证三年没检怎么办 银行工作人员态度不好怎么办 生殖保健服务证怎么办 关机后自动开机怎么办 大连卖房户口怎么办 在警务室被辅警骂了怎么办