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