tableview折叠效果

//

//  MyData.h

//  tableview折叠效果

//

//  Created by apple on 16/4/21.

//  Copyright © 2016 崔家维. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface MyData : NSObject

{

    NSMutableArray * _array;

    BOOL    _isShow;

    NSString  * _name;

}

@property (nonatomic,strong) NSMutableArray * array;

@property (nonatomic,copy) NSString * name;

@property (nonatomic,assign) BOOL isShow;

@end



//

//  MyData.m

//  tableview折叠效果

//

//  Created by apple on 16/4/21.

//  Copyright © 2016 崔家维. All rights reserved.

//


#import "MyData.h"


@implementation MyData

- (instancetype)init

{

    self = [super init];

    if (self) {

        self.array = [[NSMutableArray alloc]init];

        self.name = [[NSString alloc]init];

        self.isShow = NO;

    }

    return self;

}

@end



//

//  ViewController.m

//  tableview折叠效果

//

//  Created by apple on 16/4/21.

//  Copyright © 2016 崔家维. All rights reserved.

//


#import "ViewController.h"

#import "MyData.h"

#define SEECEND_SIZE  self.view.bounds.size

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataArray;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _dataArray = [[NSMutableArray alloc]init];

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

        MyData * myData=[[MyData alloc]init];

        myData.name = [NSString stringWithFormat:@"%c",i];

        for (int j=0; j<10; j++) {

            [myData.array addObject:[NSString stringWithFormat:@"%c--%d",i,j]];

            

        }

        [self.dataArray addObject:myData];

    }

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SEECEND_SIZE.width, SEECEND_SIZE.height) style:UITableViewStyleGrouped];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    self.tableView.rowHeight = 44;

    [self.view addSubview:self.tableView];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return  self.dataArray.count;

}

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

{

    MyData * data=[self.dataArray objectAtIndex:section ];

    if ([data isShow]) {

        return data.array.count;

    }

    else {

        return 0;

    }

}

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

{

    static NSString * cellName  = @"cellID";

    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellName];

    if (!cell) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];

    }

    MyData * data=[self.dataArray objectAtIndex:indexPath.section];

    NSString * string=[[data array] objectAtIndex:indexPath.row];

    cell.textLabel.text = string;

    return  cell;

    

}

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

{

    return 44;

}

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

{

    MyData * data=[self.dataArray objectAtIndex:section];

    UIButton * button =[UIButton buttonWithType:UIButtonTypeSystem];

    button.frame =CGRectMake(0, 0, SEECEND_SIZE.width, 44);

    button.tag = section;

    [button setTitle:data.name forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    button.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1];

    return button;

}


-(void)buttonClick:(UIButton *)btn

{

    MyData * data = [self.dataArray objectAtIndex:btn.tag];

    if ([data isShow]) {

        [data setIsShow:NO];

    }

    else{

        [data setIsShow:YES];

    }

    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end







































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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值