tableView 折叠效果

#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 = [super init]) {

        

        dataArray = [NSMutableArray array];

        

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

            

            tableData * data = [[tableData alloc]init];

            data.title = [NSString stringWithFormat:@"%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.view addSubview:_infoTableView];

        

    }

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [self setData];

    [self setUI];

}

-(void)setData{


    

}

-(void)setUI{


    self.view.backgroundColor = [UIColor grayColor];

   

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

   

    self.infoTableView = tableView;

    

}


#pragma mark tableView 代理方法


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


    return dataArray.count;

}

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


    if ([[dataArray objectAtIndex:section] isShow]) {

        

         return [[[dataArray objectAtIndex: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 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: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 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCR_WIDTH, 30)];

    label.backgroundColor = [UIColor orangeColor];

    label.tag = 1000 + section;

    

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@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 ([[dataArray objectAtIndex:section] isShow]) {

        return 0;

    }else{

    

        return 1;

    }

}

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


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

    int tag = label.tag - 1000;

    tableData * data = [dataArray objectAtIndex:tag];

    data.isShow = !data.isShow;

    [dataArray replaceObjectAtIndex:tag withObject:data];

    [self.infoTableView reloadData];

    

    

}

@end



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值