类似QQ分组的样子,实现tableView的折叠与展开。其实要做这个效果我先想到的是在tableView中再嵌套多个tableView,这个想法实现起来就有点难了。

所以还是换个思路,把tableView的HeaderView用上了。给headerView加上手势,轻松解决折叠展开的问题。
直接上代码吧。
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *listArray; // 数据源
@property (nonatomic, strong) NSMutableArray *titlesArray; // 分组的名称
@property (nonatomic, strong) NSMutableDictionary *openSectionDict; // 记录哪个组展开
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化tableView
_myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.openSectionDict = [[NSMutableDictionary alloc] init]; // 初始化字典
[self setUpData];
}
// 给数据源赋值
- (void)setUpData {
self.listArray = [NSMutableArray new];
self.titlesArray = [NSMutableArray new];
for (int i = 0; i < 5; i++) { // 5个section
[self.titlesArray addObject:[NSString stringWithFormat:@"section %d", i]];
NSMutableArray *array = [NSMutableArray new];
for (int i = 0; i < 4; i++) { // 每个section有4个row
[array addObject:[NSString stringWithFormat:@"row %d", i]];
}
[self.listArray addObject:array];
}
}
// 实现tableView的代理方法
#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) { //根据记录的展开状态设置row的数量
return 0;
} else {
return 4;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CELL_ID"];
cell.textLabel.text = [NSString stringWithFormat:@"row %ld", indexPath.row];
}
return cell;
}
#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
view.backgroundColor = [UIColor whiteColor];
view.tag = KTAG + section;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, view.bounds.size.width, view.bounds.size.height)];
label.text = self.titlesArray[section];
[view addSubview:label];
if ([[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", section]] integerValue] == 0) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 10) / 2, 7, 10)];
imageView.image = [UIImage imageNamed:@"Triangle_right_gray"]; // 三角形小图片
[view addSubview:imageView];
} else {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (view.bounds.size.height - 7) / 2, 10, 7)];
imageView.image = [UIImage imageNamed:@"Triangle_down_gray"];
[view addSubview:imageView];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collegeTaped:)];
[view addGestureRecognizer:tap];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.1;
}
#pragma mark - sectionHeader clicked
- (void)collegeTaped:(UITapGestureRecognizer *)sender {
NSString *key = [NSString stringWithFormat:@"%ld", sender.view.tag - KTAG];
// 给展开标识赋值
if ([[self.openSectionDict objectForKey:key] integerValue] == 0) {
[self.openSectionDict setObject:@"1" forKey:key];
} else {
[self.openSectionDict setObject:@"0" forKey:key];
}
NSUInteger index = sender.view.tag;
NSIndexSet *set = [NSIndexSet indexSetWithIndex:index - KTAG];
[self.myTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}
最后的效果:
以上所述是小编给大家介绍的iOS开发中TableView类似QQ分组的折叠与展开效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# tableview展开和收缩
# iOS实现单元格折叠
# iOS实现列表折叠效果
# iOS实现图片折叠效果
# 基于iOS实现图片折叠效果
# iOS实现折叠单元格
# 小编
# 角形
# 是在
# 多个
# 在此
# 要做
# 给大家
# 我先
# 换个
# 所述
# 给我留言
# 感谢大家
# 疑问请
# 有任何
# 中再
# addObject
# section
# NSString
# row
# array
相关文章:
建站ABC备案流程中有哪些关键注意事项?
如何用免费手机建站系统零基础打造专业网站?
如何快速搭建高效WAP手机网站?
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
免费视频制作网站,更新又快又好的免费电影网站?
制作电商网页,电商供应链怎么做?
如何快速搭建支持数据库操作的智能建站平台?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
如何通过二级域名建站提升品牌影响力?
如何高效搭建专业期货交易平台网站?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
网站制作公司排行榜,四大门户网站排名?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
如何用IIS7快速搭建并优化网站站点?
公司网站的制作公司,企业网站制作基本流程有哪些?
如何在建站之星网店版论坛获取技术支持?
建站主机选购指南:核心配置与性价比推荐解析
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
完全自定义免费建站平台:主题模板在线生成一站式服务
网站按钮制作软件,如何实现网页中按钮的自动点击?
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
北京网页设计制作网站有哪些,继续教育自动播放怎么设置?
如何快速上传自定义模板至建站之星?
建站之星2.7模板:企业网站建设与h5定制设计专题
如何快速搭建高效WAP手机网站吸引移动用户?
平台云上自主建站:模板化设计与智能工具打造高效网站
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
如何自定义建站之星网站的导航菜单样式?
制作网站的模板软件,网站怎么建设?
如何在Windows虚拟主机上快速搭建网站?
C++如何将C风格字符串(char*)转换为std::string?(代码示例)
c# F# 的 MailboxProcessor 和 C# 的 Actor 模型
广平建站公司哪家专业可靠?如何选择?
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
定制建站如何定义?其核心优势是什么?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
安云自助建站系统如何快速提升SEO排名?
如何快速搭建高效香港服务器网站?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
网站制作的方法有哪些,如何将自己制作的网站发布到网上?
如何用wdcp快速搭建高效网站?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?
阿里云网站制作公司,阿里云快速搭建网站好用吗?
如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?
ppt在线制作免费网站推荐,有什么下载免费的ppt模板网站?
云南网站制作公司有哪些,云南最好的招聘网站是哪个?
武清网站制作公司,天津武清个人营业执照注销查询系统网站?
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
香港服务器选型指南:免备案配置与高效建站方案解析
*请认真填写需求信息,我们会在24小时内与您取得联系。