#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
self.window.rootViewController = navi; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h" @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableDictionary *dataDic;
}
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
// 初始化_tableView
[self initializeTableView];
// 加载数据
[self loadData];
}
/**
* 初始化_tableView
*/
- (void)initializeTableView
{
_tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
/**
* 加载数据
*/
- (void)loadData
{
// 数组的第一个对象为标志位(是否展开)
NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"",@"apple",@"alex",@"alert",@"awake", nil];
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"",@"blance",@"bank",@"baby",@"bet",@"balance", nil];
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"",@"cake",@"cat",@"caught",@"cell",@"clabe", @"cry",@"cave",nil];
NSMutableArray *arr4 = [NSMutableArray arrayWithObjects:@"",@"dog",@"data",@"date",@"drive",@"down", @"deliver",@"dire",@"drawn",@"dety",@"depature",@"dom",nil];
NSMutableArray *arr5 = [NSMutableArray arrayWithObjects:@"",@"elphance",@"eleven",@"every",nil];
// 把对应的数据存入字典
dataDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"A",arr2,@"B",arr3,@"C",arr4,@"D",arr5,@"E", nil] ;
} #pragma mark - UITableViewDataSource And UITableViewDelegate -
// 返回的表头个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[dataDic allKeys] count];
}
// 每个表头返回对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 因为字典是无序的,通过比较来进行排序
NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSString *key = keys[section];
NSMutableArray *array = dataDic[key];
NSString *IsExpand = array[];
// 如果为“1”则返回相应的行数,否则返回0
if ([IsExpand isEqualToString:@""]) {
// 因为数组的第一位为标志位所以减1
return ([array count] - );
}
return ;
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return ;
} - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return ;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
view.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width - , )];
label.font = [UIFont systemFontOfSize:];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
label.text = keys[section];
[view addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, , [UIScreen mainScreen].bounds.size.width, );
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = + section;
[view addSubview:button];
return view;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSString *key = keys[indexPath.section];
NSMutableArray *array = dataDic[key];
// 由于数组的第一位是标志位,且不用显示,所以加1
NSString *textStr = array[indexPath.row + ];
cell.textLabel.text = textStr;
return cell;
} #pragma mark -Target Action-
/**
* 点击表头,如果没有展开,则展开;如果已经展开,则关闭
*/
- (void)buttonAction:(UIButton *)sender
{
long section = sender.tag - ;
NSArray *keys = [[dataDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSString *key = keys[section];
NSMutableArray *array = dataDic[key];
NSString *IsExpand = array[];
// 如果IsExpand等于“0”(关闭状态),则展开,且设置其值为“1”;相反,如果IsExpand等于“1”(展开状态),则关闭,且设置其值为“0”
if ([IsExpand isEqualToString:@""]) {
array[] = @"";
}else{
array[] = @"";
}
[_tableView reloadData];
}
@end