自己写的UITableView的折叠效果

//

//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值