仿QQ联系人的TableView的折叠与拉伸

仿QQ联系人的TableView的折叠与拉伸

大家好,又见面了,我是全栈君。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107830.html原文链接:https://javaforall.net

(0)
上一篇 2022年3月12日 下午3:35
下一篇 2022年3月12日 下午4:00


相关推荐

  • SEIR模型案例_SOR模型

    SEIR模型案例_SOR模型SEIR模型案例原理说明https://zhuanlan.zhihu.com/p/142117573%N是群体总样本数E=0;%潜伏期人数I=1;%患者人数S=N-I;%易感者人数

    2022年8月1日
    7
  • 【Mybatis框架】输出映射resultType与resultMap

    【Mybatis框架】输出映射resultType与resultMap如果你搜索只是返回一个值,比如说String ,或者是int,那你直接用resultType就行了。但是你如果是返回一个复杂的对象,就必须定义好这个对象的resultMap的result map。如果你搜索只是返回一个值,比如说String ,或者是int,那你直接用resultType就行了。但是你如果是返回一个复杂的对象,就必须定义好这个对象的resultMap的result …

    2022年6月13日
    32
  • SQLite Database Browser 数据库工具

    SQLite Database Browser 数据库工具SQLiteDatabaseBrowser是一个SQLite数据库管理工具。是开源的、免费的。HomePagehttp://sqlitebrowser.sourceforge.net/Downloadhttp://sourceforge.net/project/showfiles.php?group_id=87946Wikihttp://en.wikipedi…

    2025年10月16日
    6
  • maven项目使用mybatis插件Free Mybatis plugin「建议收藏」

    maven项目使用mybatis插件Free Mybatis plugin「建议收藏」首先安装插件如图所示选择第一个插件安装,安装完毕重启IDEA 然后我们选择我们已经配置好的数据库如下图在需要生成配置文件的数据库上右键,就会出现mybatis-generator选项打开这个就打开如上图所示配置窗口,这里我们使用默认的就行,如果不想使用lombok去掉这个勾选即可,然后我们选择ok即可就会生成以上文件,一个数据库的映射类,一个操作数据库的映射接口,和具体操作数据库的xml,主配置文件就在Configure里面这个自己生成…

    2026年4月17日
    5
  • intellijidea激活码2021【2021最新】

    (intellijidea激活码2021)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月21日
    68
  • MAC IntelliJ IDEA 2018.1版本 安装+破解方法

    一、下载IntelliJIDEA2018.1链接:https://confluence.jetbrains.com/display/IDEADEV/IDEA+2018.1+EAPideaIU-181.4445.20.dmg安装。二、下载包:http://idea.lanyus.com/jar/JetbrainsCrack-2.7-release-str.jar在Finder应用程序文件夹中右击…

    2022年4月8日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号