tableView 折叠效果

来源:互联网 发布:速达软件数据库密码 编辑:程序博客网 时间:2024/04/30 15:48

#import <Foundation/Foundation.h>


@interface tableData : NSObject


@property (nonatomic,copy)NSString * title;

@property (nonatomic,strong)NSMutableArray * array;


@property (nonatomic,assign,readwrite)BOOL isShow;


@end




#import "aboutViewController.h"

#import "Header.h"

#import "tableData.h"


@interface aboutViewController ()<UITableViewDataSource,UITableViewDelegate>{


    NSMutableArray * dataArray;

    

}



@property (nonatomic,strong)UITableView * infoTableView;


@end


@implementation aboutViewController


-(instancetype)init{


    if (self = [superinit]) {

        

        dataArray = [NSMutableArrayarray];

        

        for (int i ='A'; i <= 'Z' ; i++) {

            

            tableData * data = [[tableDataalloc]init];

            data.title = [NSStringstringWithFormat:@"%d",i];

            

            for (int a =0; a<6; a++) {

            

                [data.array addObject:[NSString stringWithFormat:@"%d",a]];

                

            }

            [dataArray addObject:data];

            

            

        }

        

    }

    return self;

}

-(void)setInfoTableView:(UITableView *)infoTableView{


    if (_infoTableView != infoTableView) {

        _infoTableView = infoTableView;

        _infoTableView.dataSource =self;

        _infoTableView.delegate =self;

        [self.viewaddSubview:_infoTableView];

        

    }

}


- (void)viewDidLoad {

    [superviewDidLoad];

    [self setData];

    [self setUI];

}

-(void)setData{


    

}

-(void)setUI{


    self.view.backgroundColor = [UIColorgrayColor];

   

    UITableView * tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0, SCR_WIDTH,SCR_HEIGHT)style:UITableViewStylePlain];

   

    self.infoTableView = tableView;

    

}


#pragma mark tableView 代理方法


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{


    returndataArray.count;

}

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


    if ([[dataArrayobjectAtIndex:section] isShow]) {

        

         return [[[dataArrayobjectAtIndex:section] array] count];

        

    }else{

    

        return 0;

    }

   

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 80;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString * cellid =@"cellid";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];

    

    if (!cell) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellid];

    }

    cell.textLabel.text =@"test";

    

    return cell;

    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    NSLog(@"%@",indexPath);

}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    UILabel * label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, SCR_WIDTH,30)];

    label.backgroundColor = [UIColororangeColor];

    label.tag = 1000 + section;

    

    UITapGestureRecognizer * tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapClick:)];

    label.userInteractionEnabled =YES;

    tap.numberOfTapsRequired =1;

    tap.numberOfTouchesRequired =1;

    [label addGestureRecognizer:tap];

    label.text = @"点我";

    

    return label;

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{


    return 44;

    

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{


    if ([[dataArrayobjectAtIndex:section] isShow]) {

        return 0;

    }else{

    

        return 1;

    }

}

-(void)tapClick:(UITapGestureRecognizer*)sender{


    UILabel * label = (UILabel *)sender.view;

    int tag = label.tag -1000;

    tableData * data = [dataArrayobjectAtIndex:tag];

    data.isShow = !data.isShow;

    [dataArrayreplaceObjectAtIndex:tag withObject:data];

    [self.infoTableViewreloadData];

    

    

}

@end



0 0
原创粉丝点击