//
// ZZSettingViewController.h
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/10/9.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import <UIKit/UIKit.h>
@class ZZSettingGroup;
@interface ZZSettingViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *groups;
- (ZZSettingGroup *)addGroup;
@end
//
// ZZSettingViewController.m
// ZZ_APP主流框架
//
// Created by ZZ_Macpro on 15/10/9.
// Copyright (c) 2015年 ZZ_Macpro. All rights reserved.
//
#import "ZZSettingViewController.h"
#import "ZZSettingGroup.h"
#import "ZZSettingCell.h"
#import "ZZSettingArrowItem.h"
#import "ZZSettingCheckItem.h"
#import "ZZSettingCheckGroup.h"
#define ZZCellMargin 6
@interface ZZSettingViewController ()
@end
@implementation ZZSettingViewController
- (NSMutableArray *)groups
{
if (_groups == nil) {
_groups = [NSMutableArray array];
}
return _groups;
}
- (ZZSettingGroup *)addGroup
{
ZZSettingGroup *group = [ZZSettingGroup group];
[self.groups addObject:group];
return group;
}
- (id)initWithStyle:(UITableViewStyle)style
{
return [super initWithStyle:UITableViewStyleGrouped];
}
- (id)init
{
return [super initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.tableView.backgroundView = nil;
self.view.backgroundColor = ZZGlobalBg;
self.tableView.sectionHeaderHeight = 0; // 每一组的头部高度
self.tableView.sectionFooterHeight = ZZCellMargin; // 每一组的尾部高度
// 底部控件
UIView *footer = [[UIView alloc] init];
footer.frame = CGRectMake(0, 0, 0,1);
self.tableView.tableFooterView = footer;
if (iOS7) {
self.tableView.contentInset = UIEdgeInsetsMake(ZZCellMargin - 33, 0, 0, 0);
} else {
self.tableView.contentInset = UIEdgeInsetsMake(ZZCellMargin, 0, 0, 0);
}
}
#pragma mark ------ Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ZZSettingGroup *group = self.groups[section];
return group.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ZZSettingCell *cell = [ZZSettingCell cellWithTableView:tableView];
cell.indexPath = indexPath;
ZZSettingGroup *group = self.groups[indexPath.section];
cell.item = group.items[indexPath.row];
return cell;
}
#pragma mark ------ 代理
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
ZZSettingGroup *group = self.groups[section];
return group.footer;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
ZZSettingGroup *group = self.groups[section];
return group.header;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// 0.取出模型
ZZSettingGroup *group = self.groups[indexPath.section];
ZZSettingItem *item = group.items[indexPath.row];
// 1.操作
if (item.option) {
item.option();
}
// 2.跳转
if ([item isKindOfClass:[ZZSettingArrowItem class]]) {
ZZSettingArrowItem *arrowItem = (ZZSettingArrowItem *)item;
if (arrowItem.destVcClass) {
UIViewController *destVc = [[arrowItem.destVcClass alloc] init];
destVc.title = arrowItem.title;
if (arrowItem.readyForDestVc) { // 控制器的准备工作
arrowItem.readyForDestVc(arrowItem, destVc);
}
[self.navigationController pushViewController:destVc animated:YES];
}
}
// 3.check 打勾
if ([item isKindOfClass:[ZZSettingCheckItem class]]) {
ZZSettingCheckGroup *checkGroup = (ZZSettingCheckGroup *)group;
checkGroup.checkedIndex = indexPath.row;
// 刷新
[tableView reloadData];
}
}
@end