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

折叠时:                        展开后:

     

  具体的代码如下:

 #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

最新文章

  1. 移动站应该尝试百度MIP的五个原因
  2. logrotate机制与原理[转载]
  3. 浅析jquery ui的datepicker组件
  4. 记Ubuntu开机黑屏及解决过程
  5. FastReport 隐藏matrix的列如何实现
  6. openssh for windows安装
  7. Activity的四种启动模式和onNewIntent()
  8. js中forEach无法跳出循环?
  9. 找啊找啊找GF
  10. 一步步学习ASP.NET MVC3 (15)&mdash;&mdash;过滤器
  11. WinCe 如何使应用程序只开启一个
  12. SpringMvc之java文件下载
  13. C++用new创建对象和不用new创建对象的区别解析
  14. A - Piece of Cake Kattis - pieceofcake (数学)
  15. html5(五)拖放
  16. linux系统上项目部署
  17. 如何学好C、C++语言
  18. Microsoft Azure
  19. 【CUDA学习】__syncthreads的理解
  20. goldengate一些參数整理

热门文章

  1. jquery datatable的详细用法
  2. Centos bash: make: command not found
  3. Robot Framework + Selenium2Lib
  4. MySQL表设计:每一种商品有不确定个数的属性
  5. 《java虚拟机》----类加载机制
  6. DateFormat 线程安全
  7. SpringMVC框架 注解 (转)
  8. hdu 4819 二维线段树模板
  9. 【BZOJ 1969】 1969: [Ahoi2005]LANE 航线规划 (树链剖分+线段树)
  10. BZOJ 3196 Tyvj 1730 二逼平衡树 树套树 线段树 treap