虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦。为了解决这个麻烦,可以将分组的行折叠和展开。折叠时,行内容就会隐藏起来;展开时,行内容就会显示出来。

折叠时:                        展开后:

     

  具体的代码如下:

 #import "ViewController.h"

 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSArray *provinces;
@property (strong,nonatomic)NSDictionary *cities;
@property (strong,nonatomic)NSMutableArray *Cellstates;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//初始化
self.provinces = [NSArray array];
self.cities = [[NSDictionary alloc]init];
self.Cellstates = [NSMutableArray arrayWithCapacity:self.provinces.count]; //加载数据
NSString *path = [[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];
NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path]; if(dic)
{
//所有的省份
self.provinces = [dic objectForKey:@"provinces"]; //所有的城市
self.cities = [dic objectForKey:@"cities"];
} //默认每一个section都是折叠的
for(int i=; i<self.provinces.count; i++)
{
NSNumber *state = [NSNumber numberWithBool:NO];
[self.Cellstates addObject:state];
} //设置数据源和代理
self.tableView.dataSource = self;
self.tableView.delegate = self; } #pragma mark -tableView的数据源方法
//有多少个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.provinces.count;
}
//每个分组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([self.Cellstates[section] boolValue]) //展开的
{
//取出所有的城市
NSArray *cities = [self.cities objectForKey:self.provinces[section]];
return cities.count;
}
else //折叠的
{
return ;
}
}
//设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"citiesCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.如果没有找到,自己创建单元格对象
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
//3.设置单元格对象的内容
//取出所有的城市
NSArray *cities = [self.cities objectForKey:self.provinces[indexPath.section]];
cell.textLabel.text = cities[indexPath.row];
//设置字体颜色
cell.textLabel.textColor = [UIColor orangeColor]; return cell;
}
//设置头部标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.provinces[section];
}
#pragma mark -tableView的代理方法
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [[UIButton alloc]init]; //设置标题
[button setTitle:self.provinces[section] forState:UIControlStateNormal]; //设置颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; //设置对齐方式
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; //设置字体大小
button.titleLabel.font = [UIFont systemFontOfSize:]; //添加事件
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; //记住button的tag
button.tag = section; return button;
} #pragma mark -按钮的事件响应
-(void)buttonClicked:(UIButton*)sender
{
//1.取出旧状态
NSNumber *oldState = [self.Cellstates objectAtIndex:sender.tag]; //2.创建新状态
NSNumber *newState = [NSNumber numberWithDouble:![oldState boolValue]]; //3.删除旧状态
[self.Cellstates removeObjectAtIndex:sender.tag]; //4.添加新状态
[self.Cellstates insertObject:newState atIndex:sender.tag]; //刷新表格
[self.tableView reloadData];
}
@end

iOS:分组的表格视图UITableView,可以折叠和展开的更多相关文章

  1. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  2. iOS:带主标题、副标题、图像类型的表格视图UITableView

    制作一个通讯录,包括姓名.电话.头像,将表格视图类型设置为UITableViewCellStyleSubtitle 效果图: //创建一个联系人的类,初始化数据 在视图控制器中实现表格内容的显示 #i ...

  3. iOS:UITableView表格视图控件

    UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格)    对表格的操作主要 ...

  4. IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)

    复杂模型分组展示 #import "HMViewController.h" #import "HMCarGroup.h" #import "HMCar ...

  5. UITableView表格视图、UITableView代理方法及应用

    一.基本知识点 UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列. 使用UITableView: 1.展示信 ...

  6. IOS中表视图(UITableView)使用详解

    IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...

  7. IOS 表视图UITableView 束NSBundle

    今天搞了一下表视图UITableView 表视图是在以后应用程序开发中经常用到的一个视图,所以必须要熟练掌握 所获不多,对视图有了一个大概的了解 其中有用到NSBundle , 束   这个类 先说一 ...

  8. 【转】 iOS如何实现表格的折叠效果?

    原文 :  http://blog.csdn.net/youcanping2008/article/details/9202167 一.实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了 ...

  9. iOS开发高级分享 - iOS的可折叠表视图

    导言 我曾经开发过一个iphone应用程序,它显示了大量的输入,这些输入分为不同的类别,在`UITableView`...若要更改其中一个输入的值,用户按下表视图中的对应行,并在出现的单独屏幕中更改该 ...

随机推荐

  1. JS函数学习

    =============数学函数========== 1.Math.random()为取随机数0~1之间的:0可以取到,1取不到 alert(Math.random()); 2.Math.PI为3. ...

  2. ASP.NET WebAPI 06 HttpMessageHandler管道

    HttpMessageHandler管道 在Web API的中,微软为了更好的进行架构扩展,采用的了一套管道设计----HttpMessageHander(其实WCF也有类似架构). 在整个管道中的头 ...

  3. 【LOJ】#2010. 「SCOI2015」小凸解密码

    题解 断环为链,把链复制两份 用set维护一下全是0的区间,然后查找x + n / 2附近的区间,附近各一个过不去,最后弃疗了改为查附近的两个,然后过掉了= = 熟练掌握stl的应用,你值得拥有(雾 ...

  4. pyqt5简单登陆界面

          登陆界面姓名输入错误会弹出错误信息.正确就会弹出第二个窗体. # -*- coding:utf-8 -*- import sys from PyQt5.QtWidgets import Q ...

  5. LoadRunner:VuGen开发脚本步骤(二)

    一.介绍 Loadrunner的场景能够描述在测试活动中发生的各种事件.一个场景包括一个运行虚拟用 户活动的Load Generator 机器列表,一个测试脚本的列表以及大量的虚拟用户和虚拟用户组 二 ...

  6. win7下docker环境centos容器中安装mysql5.7

    docker环境基于镜像skiychan/nginx-php7,进行安装 ps:skiychan/nginx-php7此镜像已封装nginx1.15.3+php7.2.9 1.环境配置 配置共享文件夹 ...

  7. es6导入导出模块

    在JavaScript ES6中,export与export default均可用于导出常量.函数.文件.模块等,你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方 ...

  8. chakra在vs2017中编译出现的问题

    转:http://blog.csdn.net/ink_cherry/article/details/73437981 1.无法找到vs2010生成工具 MSB8020 无法找到 Visual Stud ...

  9. Bakery CodeForces - 707B (最短路的思路题)

    Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...

  10. android studio 目录 文件 解读