您的位置:首页 > 产品设计 > UI/UE

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: