自己写的UITableView的折叠效果

来源:互联网 发布:软件设计师通过率 编辑:程序博客网 时间:2024/06/05 04:19

//

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

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    

    NSString *path  = [[NSBundlemainBundle] pathForResource:@"ExpansionTableTestData"ofType:@"plist"];

    _datalist = [[NSMutableArrayalloc] initWithContentsOfFile:path];

    NSLog(@"%@",path);

  //  NSLog(@"data = %@",_datalist);

    

    _tableView =  [[UITableViewalloc] initWithFrame:CGRectMake(0,0, 320,460) style:UITableViewStylePlain];

    _tableView.delegate =self;

    _tableView.dataSource =self;

    _tableView.autoresizingMask =UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    _tableView.backgroundColor = [UIColorclearColor];

    [self.viewaddSubview:_tableView];

    

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [_datalistcount];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    //判断当前section开关状态:YES(  NO()

    BOOL select = [(NSNumber*)[[_datalistobjectAtIndex:section] objectForKey:@"Select"]boolValue];

    

    if (select == YES)

    {

        return [[[_datalistobjectAtIndex:section] objectForKey:@"list"]count]+1;

    }

    return 1;

}


- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    BOOL select = [(NSNumber*)[[_datalistobjectAtIndex: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*)[[_datalistobjectAtIndex:indexPath.section] objectForKey:@"Select"]boolValue];


    if (select == YES&&indexPath.row !=0)

    {

        //子类cell

        ChildrenCell *cell = (ChildrenCell*)[tableViewdequeueReusableCellWithIdentifier:nil];

        if (cell == nil) {

            cell = [[ChildrenCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

        }else{

            while ([cell.contentView.subviewslastObject] != nil) {

                [(UIView*)[cell.contentView.subviewslastObject] removeFromSuperview]; //删除并进行重新分配

            }

        }

        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        

        NSArray *list = [[_datalistobjectAtIndex:indexPath.section] objectForKey:@"list"];

        cell.titleLabel.text = [listobjectAtIndex:indexPath.row-1];

        

        if (indexPath.row ==1)

        {

            UIImageView *upimg = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 320,5)];

            upimg.backgroundColor = [UIColorclearColor];

            upimg.image = [UIImageimageNamed:@"down.png"];

            [cell.contentView addSubview:upimg];

        }

        if (indexPath.row == [listcount])

        {

            UIImageView *upimg = [[UIImageViewalloc]initWithFrame:CGRectMake(0,75, 320,5)];

            upimg.backgroundColor = [UIColorclearColor];

            upimg.image = [UIImageimageNamed:@"up.png"];

            [cell.contentView addSubview:upimg];

        }

     

        return cell;

    }else

    {

        //父类cell

        SuperCell *cell = (SuperCell*)[tableViewdequeueReusableCellWithIdentifier:nil];

        if (cell == nil) {

            cell = [[SuperCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

        }else{

            while ([cell.contentView.subviewslastObject] != nil) {

                [(UIView*)[cell.contentView.subviewslastObject] removeFromSuperview]; //删除并进行重新分配

            }

        }

        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        

      //  [cell changeArrowWithUp:NO];

        

        NSString *name = [[_datalistobjectAtIndex: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.tableViewbeginUpdates];

        

        BOOL select = [(NSNumber*)[[_datalistobjectAtIndex:indexPath.section] objectForKey:@"Select"]boolValue];

        

      //  SuperCell *cell = (SuperCell*)[self.tableView cellForRowAtIndexPath:indexPath];

        

        if (select == 0)

        {

          //  [cell changeArrowWithUp:YES];

            

            [self OnChildrenCell:indexPath.sectionUp:select];

            

        } else

        {

         //   [cell changeArrowWithUp:NO];

            

            [self OffChildrenCell:indexPath.sectionUp:select];

        }

        //动画结束

        [self.tableViewendUpdates];

        

    }else

    {

        //子层触发事件

        NSDictionary *dic = [_datalistobjectAtIndex:indexPath.section];

        NSArray *list = [dic objectForKey:@"list"];

        NSString *item = [list objectAtIndex:indexPath.row-1];

        UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:@"取消"otherButtonTitles: nil];

        [alert show];

    }

    

}


//打开子列表

-(void)OnChildrenCell:(NSUInteger)index Up:(BOOL)up

{

    [[_datalistobjectAtIndex:index] removeObjectForKey:@"Select"];

    [[_datalistobjectAtIndex:index] setObject:@"1"forKey:@"Select"];

    

    NSMutableArray * indexPathTosection = [NSMutableArrayarray];

    for (int i = 1; i<[[[_datalist objectAtIndex:index]objectForKey:@"list"] count]+1; i++)

    {

        NSIndexPath *indexPath = [NSIndexPathindexPathForRow:i inSection:index];

        [indexPathTosection addObject:indexPath];

    }

    //插入子类cell

    [self.tableViewinsertRowsAtIndexPaths:indexPathTosection withRowAnimation:UITableViewRowAnimationTop];

    //打开方式

    [self.tableViewscrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionNoneanimated:YES];

}

//关闭子列表

-(void)OffChildrenCell:(NSUInteger)index Up:(BOOL)up

{

    [[_datalistobjectAtIndex:index] removeObjectForKey:@"Select"];

    [[_datalistobjectAtIndex:index] setObject:@"0"forKey:@"Select"];

    

    NSMutableArray * indexPathTosection = [NSMutableArrayarray];

    for (int i = 1; i<[[[_datalist objectAtIndex:index]objectForKey:@"list"] count]+1; i++)

    {

        NSIndexPath *indexPath = [NSIndexPathindexPathForRow:i inSection:index];

        [indexPathTosection addObject:indexPath];

    }

    //移除子类cell

    [self.tableViewdeleteRowsAtIndexPaths:indexPathTosection withRowAnimation:UITableViewRowAnimationTop];

}



- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

原创粉丝点击