最近手上任务比较轻,打算把一些之前做一些的简单的东西再整理整理,优化一下,做个记录;

TableView的折叠拉伸主要是在一些联系人分组里会经常见到,今天做了一个简单的demo,代码也做了处理,随拿随用。

思路整理:

1.根据数据设置分组

2.设置折叠/拉伸状态标识

3.实现分组透视图点击事件

实现代码:

#import "FoldTableViewController.h"

#define STATE @"state"

#define INFO @"info"

@interface FoldTableViewController ()

{

NSMutableArray *_dataArray;//数据源数组

}

@end

@implementation FoldTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self setExtraCellLineHidden];

[self requestData];

}

//底部视图留白

- (void)setExtraCellLineHidden{

UIView *view = [UIView new];

view.backgroundColor = [UIColor clearColor];

[self.tableView setTableFooterView:view];

}

//创建数据

- (void)requestData{

/**

假设有i组数据,每组有j条内容:

每组实例化一个可变字典存储组内数据,STATE对应当前状态(折叠/拉伸),INFO对应当前内容;

*/

_dataArray = [[NSMutableArray alloc]init];

for (int i = 0; i < 5; i++) {

NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];

NSMutableArray *tempArr = [[NSMutableArray alloc]init];

for (int j = 0; j < 10; j++) {

NSString *infoStr = [NSString stringWithFormat:@"test%d",j];

[tempArr addObject:infoStr];

}

[tempDict setValue:@"1" forKey:STATE];

[tempDict setValue:tempArr forKey:INFO];

[_dataArray addObject:tempDict];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return _dataArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSString *state = _dataArray[section][STATE];

if ([state isEqualToString:@"0"]) {

return 0;

}

return [_dataArray[section][@"info"] count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *ide = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ide];

}

//取到当前cell的内容

cell.textLabel.text = _dataArray[indexPath.section][@"info"][indexPath.row];

return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)];

headView.tag = 10+section;

headView.backgroundColor = [UIColor grayColor];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];

[headView addSubview:imageView];

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 5, 100, 20)];

titleLabel.text = [NSString stringWithFormat:@"section%ld",section];

titleLabel.textColor = [UIColor whiteColor];

[headView addSubview:titleLabel];

/**在刷新视图时,根据当前分组的状态,调整头视图的内容视图

*/

NSDictionary *dict = _dataArray[section];

if ([dict[STATE] isEqualToString:@"1"]) {

imageView.image = [UIImage imageNamed:@"arrow_spread"];

}else{

imageView.image = [UIImage imageNamed:@"arrow_fold"];

}

/**为头视图添加轻触事件

*/

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headViewClick:)];

[headView addGestureRecognizer:tap];

return headView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 30;

}

- (void)headViewClick:(UITapGestureRecognizer *)tap{

NSInteger index = tap.view.tag-10;

NSMutableDictionary *dict = _dataArray[index];

/**点击头视图,改变该组状态

*/

if ([dict[STATE] isEqualToString:@"1"]) {

[dict setValue:@"0" forKey:STATE];

}else{

[dict setValue:@"1" forKey:STATE];

}

/**刷新当前点击分组的数据

reloadSections:需要刷新的分组

withRowAnimation:刷新的动画方式

*/

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];

}

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

转载于:https://juejin.im/post/5a31d4d451882526151a9ac2

仿QQ联系人的TableView的折叠与拉伸相关推荐

  1. CSS+JS仿QQ面板风格的多级折叠下拉菜单

    <html><head><title>CSS+JS仿QQ面板风格的多级折叠下拉菜单丨石家庄玻璃隔断|石家庄自动门</title><style ty ...

  2. 仿QQ多级折叠、展开菜单,三级下拉导航

    仿QQ多级折叠.展开菜单,三级下拉导航 仿QQ面板风格的多级折叠.展开菜单,三级下拉导航,JavaScript+CSS共同结晶的结果,推荐给大家,点击"运行"可查看效果. http ...

  3. WPF编程;上位机编程;C#编程;仿QQ基础实现(一)之界面预览

    简介 一.摘要 1.描述 2.关键字 二.什么是WPF 三.为什么选择WPF 四.仿QQ的登录界面 五.仿QQ联系人界面 六.源码下载 七.其他 八.参考 一.摘要 1.描述 本文主要描述的是如何通过 ...

  4. JS+CSS打造仿QQ面板的三级折叠下拉菜单

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  5. android 仿qq好友列表分组效果及联系人分组效果

     历史记录仿QQ好友列表的动态效果 以及联系人的分组效果 QQ朋友分组的功能做的不错,大家都很认可,那么到底他的分组并且滑动的时候,标题能停留在顶部是如何实现的呢?今天从网上搜索了一下资料,自己运行了 ...

  6. iOS之仿QQ好友列表展开收缩效果的实现

    使用UICollectionView实现 思路 很明显整体它是一个列表,它的分组是一个列表,它里面的好友列表也是一个列表,所以就可以使用组头来设置分组列表,使用cell设置好友列表: 当点击组头的时候 ...

  7. 仿qq左滑删除listview_Java基于Swing和Netty仿QQ界面聊天小项目

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:硬刚一周,3W字总结,一年的经验告诉你如何准备校招! 个人原创100W+访问量博客:点击前往,查看更多 来源:b ...

  8. android qq分组展开,Android仿qq分组管理的第三方库

    本文实例为大家分享了Android仿qq分组管理的第三方库,供大家参考,具体内容如下 下面先看效果 我们点击展开与折叠分组的功能在库里面是已经封装好的,只能把它已入到项目中,就可以直接用了,十分的方便 ...

  9. java 仿qq空间_仿QQ空间和微信朋友圈,高解耦高复用高灵活

    先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 ...

最新文章

  1. OpenCV函数 Canny 检测边缘
  2. 小师妹学JVM之:JVM的架构和执行过程
  3. 信息系统项目管理系列之九:项目质量管理
  4. CSS3-新增属性选择器
  5. Scala 学习(四) 集合之List
  6. easyui下拉选项多怎么解决_30岁以后皮肤松弛皱纹越来越多怎么办?这组瑜伽帮你解决...
  7. 使用 Python 进行双重退火优化
  8. 网管工具 dstat
  9. 成功软文营销经典案例-案例分享
  10. WIN7通过mount挂载nfs配置root权限,解决不可写的问题
  11. 刷机!刷机!!刷机!!!
  12. 微信小程序如何实现搜索框的防抖功能
  13. 单片机基础-第一个单片机系统
  14. 基于C51单片机的万年历设计(LCD1602显示)
  15. 【信号源】脉冲发生器和数字码型发生器的区别
  16. 数据结构 ---- 哈夫曼树****
  17. stm32外设-RCC
  18. 转:长篇小说《七月七日晴》(超感人的)(续)
  19. 正则表达式匹配连续相同字符,如...aaa..bbb...11111...2222...
  20. matlab图像处理常用函数大全

热门文章

  1. Java实现根据地理位置获取经纬度
  2. Android: AndroidStudio使用OpenCV-Java
  3. Android测试写入文本Log
  4. Qt编写数据可视化大屏界面电子看板12-数据库采集
  5. word日常排版(页眉和页脚)
  6. 远程桌面时提示凭证不工作问题的终极解决办法
  7. Bzoj1029 [JSOI2007]建筑抢修
  8. 装了Ubuntu后将默认启动项修改为windows
  9. js弹出窗体获得焦点
  10. Windows 2003 系统管理 视频教程 http://www.91xueit.comm 下载