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

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

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

方法一

NSMutableArray *stateArray = [NSMutableArray array];for (int i = 0; i < dataSource.count; i++)
{//所有的分区都是闭合[stateArray addObject:@"0"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{if ([stateArray[section] isEqualToString:@"1"]){//如果是展开状态NSArray *array = [dataSource objectAtIndex:section];return array.count;
}else{//如果是闭合,返回0return 0;
}
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[button setTag:section+1];
button.backgroundColor = [UIColor whiteColor];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
[line setImage:[UIImage imageNamed:@"line_real"]];
[button addSubview:line];UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (kCell_Height-22)/2, 22, 22)];
[imgView setImage:[UIImage imageNamed:@"ico_faq_d"]];
[button addSubview:imgView];UIImageView *_imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-30, (kCell_Height-6)/2, 10, 6)];if ([stateArray[section] isEqualToString:@"0"]) {_imgView.image = [UIImage imageNamed:@"ico_listdown"];
}else if ([stateArray[section] isEqualToString:@"1"]) {_imgView.image = [UIImage imageNamed:@"ico_listup"];
}
[button addSubview:_imgView];UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(45, (kCell_Height-20)/2, 200, 20)];
[tlabel setBackgroundColor:[UIColor clearColor]];
[tlabel setFont:[UIFont systemFontOfSize:14]];
[tlabel setText:sectionArray[section]];
[button addSubview:tlabel];
return button;
}
- (void)buttonPress:(UIButton *)sender//headButton点击
{
//判断状态值
if ([stateArray[sender.tag - 1] isEqualToString:@"1"]){//修改[stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"0"];
}else{[stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"1"];
}
[expandTable reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];}

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

方法二

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

@interface GroupModel : NSObject
@property (nonatomic, assign)BOOL isOpened;
@property (nonatomic, retain)NSString *groupName;
@property (nonatomic, assign)NSInteger groupCount;@property (nonatomic, retain)NSArray *groupFriends;
@end

再看看怎么用

 for (NSDictionary *groupInfoDic in JSONDic[@"group"]) {GroupModel *model = [[GroupModel alloc]init];model.groupName = groupInfoDic[@"groupName"];model.groupCount = [groupInfoDic[@"groupCount"] integerValue];model.isOpened = NO;model.groupFriends = groupInfoDic[@"groupArray"];[dataSource addObject:model];
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return dataSource.count;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
GroupModel *groupModel = dataSource[section];
NSInteger count = groupModel.isOpened?groupModel.groupFriends.count:0;
return count;
}
  • (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; 
的意思是如果是展开,那么点击之后就是折叠, 
如果是折叠,点击之后就是展开。这个状态被记录下来了,而且可以不断的改变。

UITableView的折叠收缩和QQ好友分组效果相关推荐

  1. expandableListview的使用,模仿qq好友分组点击收缩扩展

    我主要讲述的是用listview实现.模仿qq好友分组点击收缩.扩展功能 这个是对listview的拓展,用法比较相似,还是需要一个适配器 MainActivitypublic class MainA ...

  2. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇-使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  3. IOS 实现QQ好友分组展开关闭功能

    贴出核心代码  主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...

  4. Qt可拖拽排序表格(类似QQ好友分组排序)

    1,简介 为了最佳体验,一个拖拽行排序的功能研究了几个小时.效果参考的QQ好友分组的排序. 网上查了下好像没有人发布QT版类似的代码,于是自己动手 QQ好友分组排序效果: 2,效果 这是最终效果图,有 ...

  5. QQ好友分组模拟小程序

    QQ好友分组:一个好友组里有多个好友,一个好友只能选择一个组,这样好友组和好友之间就是一个一对多的关系.在此程序中封装一个好友类即Buddy类,一个组类即Group类.在Buddy类有有关好友的最基本 ...

  6. js实现qq好友分组

    qq好友分组 <style>ul,h2 {padding: 0;margin: 0;background-color: wheat;}li {list-style: none;}#list ...

  7. 模仿QQ好友分组风格

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title> ...

  8. Android之实现QQ好友分组(ExpandableListView)

    在项目开发中,也许我们遇到过ListView中嵌套ListView,但谷歌建议我们最好别这样做,因此他们写好了一个ExpandableListView类,他继承ListView,可以实现ListVie ...

  9. Qt可拖拽排序表格(解决滚动条不兼容问题,类似QQ好友分组排序)

    原版链接 https://blog.csdn.net/dpsying/article/details/77206127 Qt可拖拽排序表格(类似QQ好友分组排序) 感谢博主无私分享 修改后,解决了滚动 ...

最新文章

  1. 一个Java程序员的阿里面试心得,附答案解析
  2. 浅析网站外链优劣如何判定?
  3. python怎么安装matplotlib-如何安装Python绘图库Matplotlib?
  4. easyUI的引用方式
  5. C++|Qt工作笔记-C++获取当前系统时间,Qt获取当前系统时间及各标准间转化
  6. Spring Cloud OAuth2 实现用户认证及单点登录
  7. 安全测试工具_选型必看:DevOps中的安全测试工具推荐
  8. 蓝桥杯2016年第七届C/C++省赛B组第八题-四平方和
  9. mini139聊天软件
  10. 运筹学 matlab实现单纯形法
  11. 关于神经网络中的shape问题
  12. 地磅系统连不上云端服务器,LiteCMS云称重管理系统
  13. 一段真实在个人经历, 给那些迷失方向的朋友[转帖]
  14. pta——点赞,打印杨辉三角,吃火锅(c语言)
  15. 力扣刷题笔记--304 二维区域和检索 - 矩阵不可变 前缀和
  16. Swift — UIKit 之(8)—— 持久层|用户偏好设置
  17. php--如何编写一个简易的论坛
  18. 【MySQL】MySQL表的增删改查(进阶)
  19. Java字符串:valueOf() 方法
  20. 手机云盘share php,宝塔面板安装云盘目录列表TCShare – 支持和彩云/天翼云

热门文章

  1. SNS是什么?有哪些类型的SNS网站?
  2. 关于搭建视频直播运营平台的一点经验和心得
  3. pymongo linux,centos安装PyMongo库
  4. 流星雨html5,HTML5/Canvas 流星雨+爱心绽放
  5. 【MOT评价指标】MOTA MOTAL MOTP Rcll IDF1 MT ML FP FN ID_SW Frag Hz
  6. git 批量删除 tag
  7. 299. 猜数字游戏(简单题)
  8. C++11 线程同步原语
  9. OV7620摄像头使用
  10. 全网最全可视化大屏模板