当前位置: 首页 > news >正文

[iOS] 折叠 cell

目录

前言

1.原理

2.折叠 cell 的点击选中

3.折叠 cell 高度的变化

4.实现效果

5.总结


前言

折叠 cell 是在 3GShare 中写过的一个小控件,这篇博客是一个小小的总结。

1.原理

在这里的核心就是我们可以通过改变按钮的 tag 值来判断我们是否应该展开还是回收,还有就是我们还可以通过代码来改变我们的 cell 的行数,并且可以通过- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath这个方法来实现点击单元格的切换。

2.折叠 cell 的点击选中

- (void)toggleFold {if (self.zhedie.tag == 1002) {// 折叠
//        [self.zhedie setTitle:@"展开" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向左箭头"] forState:UIControlStateNormal];self.tableView.frame = CGRectMake(270, 200, 95, 20);self.zhedie.tag = 1001;} else {// 展开
//        [self.zhedie setTitle:@"折叠" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向下箭头"] forState:UIControlStateNormal];self.tableView.frame = CGRectMake(270, 200, 95, 80);self.zhedie.tag = 1002;}[self.tableView reloadData];
}
- (void)pressUp:(UIButton *)btn {[self toggleFold];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {if (indexPath.section == 1) {self.selectedItem = self.dataArray[indexPath.row];[self toggleFold];}
}

我在这里使用了一个函数来封装我的 cell 的收缩和展开,因为在点击按钮和点击表格都要实现切换。

3.折叠 cell 高度的变化

在这里我会给出通过按钮 tag 值来改变 tableView 的高度的点击函数。

- (void)toggleFold {if (self.zhedie.tag == 1002) {// 折叠
//        [self.zhedie setTitle:@"展开" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向左箭头"] forState:UIControlStateNormal];self.tableView.frame = CGRectMake(270, 200, 95, 20);self.zhedie.tag = 1001;} else {// 展开
//        [self.zhedie setTitle:@"折叠" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向下箭头"] forState:UIControlStateNormal];self.tableView.frame = CGRectMake(270, 200, 95, 80);self.zhedie.tag = 1002;}[self.tableView reloadData];
}
- (void)pressUp:(UIButton *)btn {[self toggleFold];
}

4.实现效果

5.总结

这里我给出完整的代码

#import "ViewController.h"@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIButton *zhedie;
@property (nonatomic, strong) NSString *selectedItem;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.dataArray = [NSMutableArray arrayWithObjects:@"cell1", @"cell2", @"cell3", nil];self.selectedItem = self.dataArray.firstObject;// tableViewself.tableView = [[UITableView alloc] initWithFrame:CGRectMake(270, 200, 95, 20)style:UITableViewStylePlain];self.tableView.delegate = self;self.tableView.dataSource = self;[self.view addSubview:self.tableView];self.zhedie = [UIButton buttonWithType:UIButtonTypeSystem];self.zhedie.frame = CGRectMake(270 + 95, 200, 40, 20);self.zhedie.backgroundColor = [UIColor clearColor];
//    [self.zhedie setTitle:@"展开" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向左箭头"] forState:UIControlStateNormal];[self.zhedie setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];[self.zhedie addTarget:self action:@selector(pressUp:) forControlEvents:UIControlEventTouchUpInside];self.zhedie.tag = 1001;
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return (self.zhedie.tag == 1001) ? 1 : 2;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (self.zhedie.tag == 1001) {return 1;} else {if (section == 0) return 1;return self.dataArray.count;}
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 20;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {NSString *cellID = @"ID";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];}if (self.zhedie.tag == 1001) {cell.textLabel.text = self.selectedItem;} else {if (indexPath.section == 0) {cell.textLabel.text = self.selectedItem;cell.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];} else {cell.textLabel.text = self.dataArray[indexPath.row];cell.backgroundColor = [UIColor whiteColor];}}cell.textLabel.font = [UIFont systemFontOfSize:12.0];return cell;
}
- (void)toggleFold {if (self.zhedie.tag == 1002) {// 折叠
//        [self.zhedie setTitle:@"展开" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向左箭头"] forState:UIControlStateNormal];self.tableView.frame = CGRectMake(270, 200, 95, 20);self.zhedie.tag = 1001;} else {// 展开
//        [self.zhedie setTitle:@"折叠" forState:UIControlStateNormal];[self.zhedie setImage:[UIImage imageNamed:@"向下箭头"] forState:UIControlStateNormal];self.tableView.frame = CGRectMake(270, 200, 95, 80);self.zhedie.tag = 1002;}[self.tableView reloadData];
}
- (void)pressUp:(UIButton *)btn {[self toggleFold];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {if (indexPath.section == 1) {self.selectedItem = self.dataArray[indexPath.row];[self toggleFold];}
}
@end


文章转载自:

http://LJMSZt84.btrfm.cn
http://eOuMgUrs.btrfm.cn
http://2rqpnTt4.btrfm.cn
http://Grv5JPD0.btrfm.cn
http://f4A8thHL.btrfm.cn
http://Wv9rkR4n.btrfm.cn
http://ldkQKORi.btrfm.cn
http://6RdL4wdN.btrfm.cn
http://2TzSBRPH.btrfm.cn
http://vVMUawky.btrfm.cn
http://8yaIxwzo.btrfm.cn
http://LUH9vPLG.btrfm.cn
http://MfCCKDM8.btrfm.cn
http://90ljQP0s.btrfm.cn
http://HmSJZUPI.btrfm.cn
http://1uqL0oy6.btrfm.cn
http://5t6byAIo.btrfm.cn
http://Sx5udbsd.btrfm.cn
http://pFRNLkx0.btrfm.cn
http://h4gzet3e.btrfm.cn
http://9ucrWiTs.btrfm.cn
http://KdiA2oN5.btrfm.cn
http://RYXcOdo9.btrfm.cn
http://LgSCeuSU.btrfm.cn
http://tiKd8y2r.btrfm.cn
http://KhgZCTFh.btrfm.cn
http://CLpCSJqW.btrfm.cn
http://MwYzFWma.btrfm.cn
http://5aCnJRbB.btrfm.cn
http://gOfKqy3p.btrfm.cn
http://www.dtcms.com/a/367766.html

相关文章:

  • Qt 系统相关 - 1
  • JavaScript 实战进阶续篇:从工程化到落地的深度实践
  • 深度学习:自定义数据集处理、数据增强与最优模型管理
  • ASRPRO语音模块
  • 一个开源的企业官网简介
  • Linux的权限详解
  • 【ICCV 2025 顶会论文】,新突破!卷积化自注意力 ConvAttn 模块,即插即用,显著降低计算量和内存开销。
  • HTB Jerry
  • 微信支付--在线支付实战,引入Swagger,定义统一结果,创建并连接数据库
  • 为什么串口发送一串数据时需要延时?
  • 决策树算法详解:从原理到实战
  • 生成式AI优化新纪元:国产首个GEO工具的技术架构剖析
  • 2025年高教社杯全国大学生数学建模竞赛B题思路(2025数学建模国赛B题思路)
  • 【C语言】第一课 环境配置
  • git命令行打patch
  • day2today3夏暮客的Python之路
  • 随时学英语5 逛生活超市
  • Web相关知识(草稿)
  • 计算机组成原理:GPU架构、并行计算、内存层次结构等
  • 用服务器搭 “私人 AI 助手”:不用联网也能用,支持语音对话 / 文档总结(教程)
  • 学生时间管理系统设计与实现(代码+数据库+LW)
  • 【3D 入门-6】大白话解释 SDF(Signed Distance Field) 和 Marching Cube 算法
  • 并发编程——17 CPU缓存架构详解高性能内存队列Disruptor实战
  • Pycharm终端pip install的包都在C:\Users\\AppData\Roaming\Python\解决办法
  • Linux中用于线程/进程同步的核心函数——`sem_wait`函数
  • Day2p2 夏暮客的Python之路
  • C++虚函数虚析构函数纯虚函数的使用说明和理解
  • Process Explorer 学习笔记(第三章3.1.1):度量 CPU 的使用情况详解
  • 机器学习入门,第一个MCP示例
  • Spring Boot项目中MySQL索引失效的常见场景与解决方案