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

具体的代码如下:
#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,可以折叠和展开的更多相关文章
- IOS开发之表视图(UITableView)
IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...
- iOS:带主标题、副标题、图像类型的表格视图UITableView
制作一个通讯录,包括姓名.电话.头像,将表格视图类型设置为UITableViewCellStyleSubtitle 效果图: //创建一个联系人的类,初始化数据 在视图控制器中实现表格内容的显示 #i ...
- iOS:UITableView表格视图控件
UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格) 对表格的操作主要 ...
- IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)
复杂模型分组展示 #import "HMViewController.h" #import "HMCarGroup.h" #import "HMCar ...
- UITableView表格视图、UITableView代理方法及应用
一.基本知识点 UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列. 使用UITableView: 1.展示信 ...
- IOS中表视图(UITableView)使用详解
IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...
- IOS 表视图UITableView 束NSBundle
今天搞了一下表视图UITableView 表视图是在以后应用程序开发中经常用到的一个视图,所以必须要熟练掌握 所获不多,对视图有了一个大概的了解 其中有用到NSBundle , 束 这个类 先说一 ...
- 【转】 iOS如何实现表格的折叠效果?
原文 : http://blog.csdn.net/youcanping2008/article/details/9202167 一.实现原理:就是在点击表格组头视图的时候,如果该表格视图的组展开了 ...
- iOS开发高级分享 - iOS的可折叠表视图
导言 我曾经开发过一个iphone应用程序,它显示了大量的输入,这些输入分为不同的类别,在`UITableView`...若要更改其中一个输入的值,用户按下表视图中的对应行,并在出现的单独屏幕中更改该 ...
随机推荐
- Python2中input()、raw_input()和Python3中input()
听了ALEX的某节课,说input()和raw_input()函数在Python2中没有区别,现在来探讨一下Python2中的input().raw_input()函数和Pyhont3中的input( ...
- C++输入与输出
1 概述 C和C++都没有将输入和输出建立在语言中,C++将输入输出的解决方案放在类库中(由头文件iostream和fstream中定义的类) C++程序把输入和输出看作字节流.流充当了程序和流源流目 ...
- Java List转换为字符串的几种方法
Java List转换为字符串的几种方法 import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import ...
- fastadmin iframe 表单提交之后跳转
controller 对应的那个js文件中添加: define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, und ...
- OOD沉思录 --- 面向动作与面向对象 --- 避免泛滥成灾的类
3.7 从设计中取出不需要的类 只有Get/Set方法的类不算是一个必要的类,Get/Set方法也不算是有意义的行为.这种类降级为属性更加合适. 3.8 去除系统外部的类 如果一个类只调用系统领域的方 ...
- Anaconda 安装 OpenCV 遇到的问题
1. 使用 pip install 安装 OpenCV 2. 对于 Ananconda 安装 OpenCV ,通常会遇到无法 import 的情况, 这是由于 anaconda 本身没有遵循 PE ...
- openstack首页默认跳转
在/var/www/html新建index.html <html> <head> <meta http-equiv="Content-Language" ...
- 【51Nod 1363】最小公倍数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1363 \[ \begin{aligned} &\sum_{i=1 ...
- 「2017 山东一轮集训 Day4」基因
设置 \(\sqrt{n}\) 个关键点,维护出关键点到每个右端点之间的答案以及Pam的左指针,每次暴力向左插入元素即可,为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边 ...
- [BZOJ4817][SDOI2017]树点涂色(LCT+DFS序线段树)
4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 692 Solved: 408[Submit][Status ...