UITableView的折叠收缩和QQ好友分组效果

可折叠展开的tableView,QQ好友分组列表

demo下载地址https://github.com/zhengwenming/ExpandTableView


原理分析:这个可以折叠的table,我们点击的是table的section,每个section下面都对应一个数组,点击section,就展开sction然后展示数组数据。每次点击section都要刷新当前点击的这个section,不用reloadData,提高效率。那么点击的这个sction怎么知道自己是展开呢还是折叠起来呢?那么关键就是对这里的处理,我们自己要加一个条件判断是展开还是折叠。下面上代码看看具体实现。

方法一

方法一的原理是用一个stateArray去记录每个section的状态,当然光记录还是不行的,还是不断的改变这个stateArray对应section的值,展开了就把值替换为1,闭合了替换了0.那么这个0和1就是我们的依据,依据这个就可以返回这个scetion对应的row了。

给section中的按钮添加点击事件,然后设置按钮的tag值为section。

方法二

方法二中用一个类去记录和不断的替换状态,用一个model类。看看这个model类的定义

再看看怎么用

(UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section 

UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 

sectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8]; 

GroupModel *groupModel = dataSource[section];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

[button setFrame:sectionView.bounds]; 

[button setTag:section]; 

[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; 

[button setTitle:groupModel.groupName forState:UIControlStateNormal]; 

[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)]; 

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

[sectionView addSubview:button]; 

UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)]; 

[line setImage:[UIImage imageNamed:@”line_real”]]; 

[sectionView addSubview:line];

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)]; 

[imgView setImage:[UIImage imageNamed:@”ico_list”]]; 

[sectionView addSubview:imgView];

UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, (44-20)/2, 40, 20)]; 

[numberLabel setBackgroundColor:[UIColor clearColor]]; 

[numberLabel setFont:[UIFont systemFontOfSize:14]]; 

NSInteger onLineCount = 0; 

for (NSDictionary *friendInfoDic in groupModel.groupFriends) { 

if ([friendInfoDic[@”status”] isEqualToString:@”1”]) { 

onLineCount++; 

[numberLabel setText:[NSString stringWithFormat:@”%ld/%ld”,onLineCount,groupModel.groupCount]]; 

[sectionView addSubview:numberLabel];

return sectionView; 

- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath 

GroupModel *groupModel = dataSource[indexPath.section]; 

NSDictionary *friendInfoDic = groupModel.groupFriends[indexPath.row]; 

NSLog(@”%@ %@”,friendInfoDic[@”name”],friendInfoDic[@”shuoshuo”]); 

}

(void)buttonPress:(UIButton *)sender//headButton点击 

GroupModel *groupModel = dataSource[sender.tag]; 

groupModel.isOpened = !groupModel.isOpened; 

[expandTable reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic]; 

}

看看关键代码,就是按钮点击事件里面buttonPress,这一句话 

groupModel.isOpened = !groupModel.isOpened; 

的意思是如果是展开,那么点击之后就是折叠, 

如果是折叠,点击之后就是展开。这个状态被记录下来了,而且可以不断的改变。

好,到此为止。两种方法介绍完毕。想看demo的同学可以直接到Github上下载https://github.com/zhengwenming/ExpandTableView

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

推荐阅读更多精彩内容

  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,453评论 9 5
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,090评论 3 38
  • 插入行。删除行。折叠区。设置索引。刷新表格。注册cell。 //UITableView的ADD // NSStri...
    nothing_c阅读 212评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,547评论 1 14
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,721评论 2 7