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

折叠时:                        展开后:

     

  具体的代码如下:

 #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. Linux文件访问和日志

    一.文件系统创建一个文件的过程假设我们想要新增一个文件,此时文件系统的行为是:先确定用户对于欲新增文件的目录是否具有 w 与 x 的权限,若有的话才能新增:根据 inode bitmap 找到没有使用 ...

  2. 【LOJ】#6289. 花朵

    题解 我当时连\(n^2\)的树背包都搞不明白,这道题稳稳的爆零啊= = 然后听说这道题需要FFT--我当时FFT的板子都敲不对,然后这道题就扔了 然后,我去考了thusc--好吧,令人不愉快的经历, ...

  3. 【BZOJ】4311: 向量(线段树分治板子题)

    题解 我们可以根据点积的定义,垂直于原点到给定点构成的直线作一条直线,从正无穷往下平移,第一个碰到的点就是答案 像什么,上凸壳哇 可是--动态维护上凸壳? 我们可以离线,计算每个点能造成贡献的一个询问 ...

  4. poj2243 Knight Moves(BFS)

    题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...

  5. python 类继承

    #!/usr/bin/python # Filename: inherit.py class SchoolMember: '''Represents any school member.''' def ...

  6. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  7. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  8. PHP多个版本爆出远程DOS漏洞

    近日,php多个版本爆出远程DoS漏洞(官方编号69364),利用该漏洞构造poc发起链接,很容易导致目标主机cpu的100%占用率,绿盟科技威胁响应中心随即启动应急机制, 启动应急响应工作,总结PH ...

  9. bzoj 4097: [Usaco2013 dec]Vacation Planning

    4097: [Usaco2013 dec]Vacation Planning Description Air Bovinia is planning to connect the N farms (1 ...

  10. Problem B: 深入浅出学算法003-计算复杂度

    Description 算法复杂度一般分为:时间复杂度.空间复杂度.编程复杂度. 这三个复杂度本身是矛盾体,不能一味地追求降低某一复杂度,否则会带来其他复杂度的增加.在权衡各方面的情况下,降低时间复杂 ...