// CustomUITableViewController.h
// UI2_QQ折叠-UITableViewController
//
// Created by zhangxueming on 15/7/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface CustomUITableViewController : UITableViewController
{
int sectionState[20];//标志分区的状态
} //提供数据源
@property (nonatomic, retain)NSMutableArray *dataList; @end
//
// CustomUITableViewController.m
// UI2_QQ折叠-UITableViewController
//
// Created by zhangxueming on 15/7/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "CustomUITableViewController.h" #define FoldImageName @"arrow_fold"
#define SpreadImageName @"arrow_spread" @interface CustomUITableViewController () @end @implementation CustomUITableViewController - (instancetype)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.title = @"QQ折叠";
self.dataList = [NSMutableArray array];
}
return self;
} - (void)creatData
{
for (int i=0; i<20; i++) {
NSMutableArray *array = [NSMutableArray array];
for (int j=0; j<5; j++) {
NSString *name = [NSString stringWithFormat:@"小明%d", j+1];
[array addObject:name];
}
[_dataList addObject:array];
}
}
//设置分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataList.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//根据分区的状态返回行数
if (sectionState[section]) {
return 0;
}
return [[self.dataList objectAtIndex:section] count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [[self.dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; return cell;
} //设置分区头视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
headerView.backgroundColor = [UIColor lightGrayColor];
UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 43, self.view.frame.size.width, 3)];
lineLabel.backgroundColor = [UIColor grayColor];
[headerView addSubview:lineLabel]; //加载图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 32, 32)];
if (sectionState[section]) {//收起的状态
imageView.image = [UIImage imageNamed:FoldImageName];
}
else
{
imageView.image = [UIImage imageNamed:SpreadImageName];
}
[headerView addSubview:imageView]; //添加点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
headerView.userInteractionEnabled = YES;
[headerView addGestureRecognizer:tap];
//tag值
headerView.tag = section; return headerView;
}
//0000 0000 ==0
//0000 0001 ^
//-----------
//0000 0001 ==1 //0000 0001 ==1
//0000 0001 ^
//-----------
//0000 0000 ==0 - (void)tapClick:(UITapGestureRecognizer *)tap
{
NSLog(@"-------");
NSInteger index = tap.view.tag;
sectionState[index] ^=1;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 46;
} - (void)viewDidLoad {
[super viewDidLoad];
[self creatData];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
// AppDelegate.m
// UI2_QQ折叠-UITableViewController
//
// Created by zhangxueming on 15/7/15.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "CustomUITableViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
CustomUITableViewController *root = [[CustomUITableViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor]; return YES;
}