iOS:分组的表格视图UITableView,可以折叠和展开
2015-09-09 09:22
519 查看
虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦。为了解决这个麻烦,可以将分组的行折叠和展开。折叠时,行内容就会隐藏起来;展开时,行内容就会显示出来。
折叠时: 展开后:
具体的代码如下:
折叠时: 展开后:
具体的代码如下:
#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=0; 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 0; } } //设置每一个单元格的内容 -(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:20]; //添加事件 [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
相关文章推荐
- SOAPUI测试REST项目(九)——REST架构推理
- SOAPUI测试REST项目(八)——REST功能测试
- SOAPUI测试REST项目(七)——REST请求工作
- Studio之widget类型及继承于Wiget类型控件的事件-----监听各个GUI控件的交互事件。
- SOAPUI测试REST项目(六)——REST服务和WADL
- SOAPUI测试REST项目(五)——REST服务和WADL
- UIView
- Leetcode: Permutation Sequence
- sql_question
- PHP中关于break与continue的使用
- EasyUI Tabs
- BinaryTreePath
- Leetcode Implement Queue using Stacks
- iOS8 自定义UITabBar (使用popToViewController导致的UITabBarButton重叠的问题)
- Leetcode Implement Stack using Queues
- Android Call requires API level 11 (current min is 8)的解决方案
- Implement stack using a queue
- LCT - hdu4010 Query on The Trees
- UVA11572_Unique Snowflakes
- UVA1152_4 Values whose Sum is 0