iOS开发-自定义Cell来完成设置界面&自定义基类控制器ZZSettingViewController

//

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值