//
// FoldViewController.m
// FoldTableView
//
// Created by wuyang on 13-11-7.
// Copyright (c) 2013年 wy. All rights reserved.
//
#import "FoldViewController.h"
#import "SuperCell.h"
#import "ChildrenCell.h"
@interface FoldViewController ()
@end
@implementation FoldViewController
@synthesize datalist = _datalist;
@synthesize tableView = _tableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] pathForResource:@"ExpansionTableTestData" ofType:@"plist"];
_datalist = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(@"%@",path);
// NSLog(@"data = %@",_datalist);
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_datalist count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//判断当前section开关状态:YES(开) , NO(关)
BOOL select = [(NSNumber*)[[_datalist objectAtIndex:section] objectForKey:@"Select"] boolValue];
if (select == YES)
{
return [[[_datalist objectAtIndex:section] objectForKey:@"list"] count]+1;
}
return 1;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL select = [(NSNumber*)[[_datalist objectAtIndex:indexPath.section] objectForKey:@"Select"] boolValue];
if (select == YES)
{
if (indexPath.row > 0)
{
return 80;
}
}
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL select = [(NSNumber*)[[_datalist objectAtIndex:indexPath.section] objectForKey:@"Select"] boolValue];
if (select == YES&&indexPath.row != 0)
{
//子类cell
ChildrenCell *cell = (ChildrenCell*)[tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[ChildrenCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}else{
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; //删除并进行重新分配
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSArray *list = [[_datalist objectAtIndex:indexPath.section] objectForKey:@"list"];
cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
if (indexPath.row == 1)
{
UIImageView *upimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 5)];
upimg.backgroundColor = [UIColor clearColor];
upimg.image = [UIImage imageNamed:@"down.png"];
[cell.contentView addSubview:upimg];
}
if (indexPath.row == [list count])
{
UIImageView *upimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 75, 320, 5)];
upimg.backgroundColor = [UIColor clearColor];
upimg.image = [UIImage imageNamed:@"up.png"];
[cell.contentView addSubview:upimg];
}
return cell;
}else
{
//父类cell
SuperCell *cell = (SuperCell*)[tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[SuperCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}else{
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; //删除并进行重新分配
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// [cell changeArrowWithUp:NO];
NSString *name = [[_datalist objectAtIndex:indexPath.section] objectForKey:@"name"];
cell.titleLabel.text = name;
return cell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
//动画开始
[self.tableView beginUpdates];
BOOL select = [(NSNumber*)[[_datalist objectAtIndex:indexPath.section] objectForKey:@"Select"] boolValue];
// SuperCell *cell = (SuperCell*)[self.tableView cellForRowAtIndexPath:indexPath];
if (select == 0)
{
// [cell changeArrowWithUp:YES];
[self OnChildrenCell:indexPath.section Up:select];
} else
{
// [cell changeArrowWithUp:NO];
[self OffChildrenCell:indexPath.section Up:select];
}
//动画结束
[self.tableView endUpdates];
}else
{
//子层触发事件
NSDictionary *dic = [_datalist objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:@"list"];
NSString *item = [list objectAtIndex:indexPath.row-1];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
[alert show];
}
}
//打开子列表
-(void)OnChildrenCell:(NSUInteger)index Up:(BOOL)up
{
[[_datalist objectAtIndex:index] removeObjectForKey:@"Select"];
[[_datalist objectAtIndex:index] setObject:@"1" forKey:@"Select"];
NSMutableArray * indexPathTosection = [NSMutableArray array];
for (int i = 1; i<[[[_datalist objectAtIndex:index] objectForKey:@"list"] count]+1; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:index];
[indexPathTosection addObject:indexPath];
}
//插入子类cell
[self.tableView insertRowsAtIndexPaths:indexPathTosection withRowAnimation:UITableViewRowAnimationTop];
//打开方式
[self.tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionNone animated:YES];
}
//关闭子列表
-(void)OffChildrenCell:(NSUInteger)index Up:(BOOL)up
{
[[_datalist objectAtIndex:index] removeObjectForKey:@"Select"];
[[_datalist objectAtIndex:index] setObject:@"0" forKey:@"Select"];
NSMutableArray * indexPathTosection = [NSMutableArray array];
for (int i = 1; i<[[[_datalist objectAtIndex:index] objectForKey:@"list"] count]+1; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:index];
[indexPathTosection addObject:indexPath];
}
//移除子类cell
[self.tableView deleteRowsAtIndexPaths:indexPathTosection withRowAnimation:UITableViewRowAnimationTop];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end