仿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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何在JavaScript中使用数组方法:Mutator方法[通俗易懂]

    如何在JavaScript中使用数组方法:Mutator方法[通俗易懂]JavaScript中的数组由元素列表组成。JavaScript有许多有用的内置方法来处理数组。修改原始数组的方法称为mutator方法,返回新值或表示的方法称为accessor方法。在…

    2025年11月3日
    15
  • 在虚拟机安装ubuntu步骤(安装虚拟机的步骤)

    准备工作VMwareWorkstation15官方下载地址:https://www.vmware.com/cn/products/workstation-pro/workstation-pro-evaluation.htmlUbuntu18.04LTS镜像官方下载地址:https://ubuntu.com/download/desktop注:VMwareWorkstation15激…

    2022年4月12日
    480
  • SMO算法最通俗易懂的解释

    SMO算法最通俗易懂的解释我的机器学习教程「美团」算法工程师带你入门机器学习已经开始更新了,欢迎大家订阅~任何关于算法、编程、AI行业知识或博客内容的问题,可以随时扫码关注公众号「图灵的猫」,加入”学习小组“,沙雕博主在线答疑~此外,公众号内还有更多AI、算法、编程和大数据知识分享,以及免费的SSR节点和学习资料。其他平台(知乎/B站)也是同名「图灵的猫」,不要迷路哦~SVM通常用对偶问题来求解,这…

    2022年6月30日
    24
  • INTEGER PRIMARY KEY AUTOINCREMENT 和 INTEGER PRIMARY KEY 区别

    INTEGER PRIMARY KEY AUTOINCREMENT 和 INTEGER PRIMARY KEY 区别转自网络 地址不可考 谢谢原作者 如有冒犯 立即删除 Sqlite 中 INTEGERPRIMA 和 rowid INTEGERPRIMA 的使用 nbsp 在用 sqlite 设计表时 每个表都有一个自己的整形 id 值作为主键 插入后能直接得到该主键 nbsp 因为 sqlite 内部本来就会为每个表加上一个 rowid 这个 rowid 可以当成一个隐含的字段使用 nbsp 但

    2025年9月2日
    4
  • 计算机中的虚拟ip,如何设置电脑虚拟IP[通俗易懂]

    计算机中的虚拟ip,如何设置电脑虚拟IP[通俗易懂]2020-02-29阅读(86)“全国统一价”其本意是根据企业产品的成本、销售渠道、品牌价值、利润所得出的,是正规生产企业规范销售市场,保护品牌形象的一种控制手段。在现实生活中,生产商打出的所谓“全国统一零售价”充其量只能理解成对产品的指导价。从某种程度上2020-02-29阅读(104)1.火烧新野。曹操统一了北方后,南下攻打刘备,此时刘备投靠刘表,驻扎在新野,曹操派大将夏侯惇进攻新野,这时刘备…

    2022年10月12日
    2
  • xshell怎么配置_winedt使用教程

    xshell怎么配置_winedt使用教程一、XShell的作用二、下载XShell三、安装XShell四、使用XShell连接Linux服务器Xshell免费版官网下载地址:https://www.xshell.com/zh/free-for-home-school/如官网打不开,可以从网盘下载链接:https://pan.baidu.com/s/1NJGWZHkByakOkQpKfkc7Yg提取码:r0ds

    2025年10月17日
    5

发表回复

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

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