1.用纯代码创建上图的中设置界面
2.步骤:
2.1 建模型
2.1.1 建group模型
// 成员变量
// group的head描述
@property (nonatomic,copy) NSString* headTitle;
// group的head描述
@property (nonatomic,copy) NSString *footTitle;
// 存放 行项目item 的数组
@property (nonatomic,strong) NSArray *items;
2.1.2 建item模型,考虑到行项目,有箭头类型,开关类型,文本类型,所以建一个baseItem,其他类型继承这个baseItem
//baseItem.h文件
#import <Foundation/Foundation.h>
@interface QKBaseItem : NSObject
typedef void (^QKItemOption)();
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) QKItemOption option;
+ (instancetype)itemWithTitle:(NSString *)title Icon:(NSString *)icon;
+ (instancetype)itemWithTitle:(NSString *)title;
@end
// arrowsItem.h
#import "QKBaseItem.h"
@interface QKArrowsItem : QKBaseItem
// 点击item,需要push的目标控制器类
@property (nonatomic,assign) Class destVC;
+ (instancetype)itemWithTitle:(NSString *)title Icon:(NSString *)icon desc:(Class)destVC;
+ (instancetype)itemWithTitle:(NSString *)title desc:(Class)destVC;
@end
2.2 建控制器
开头处,图1是主设置界面,
图2是点图1的"推送和提醒"跳转的界面,
图3是点图2的开奖提醒,跳转的界面.
设置功能的每个控制器的都比较类似,可以考虑建个父类控制器,让主设置界面,推送和提醒,开奖提醒,都继承自这个父类,这样可以省去一个一个去实现tableView数据源和代理方法的麻烦
// 父类控制器.h文件
#import <UIKit/UIKit.h>
@interface QKSetingController : UITableViewController
// 暴露出来,让子类去继承使用,data存放group,group存放items(NSArray),items才存放各个行项目item
@property (nonatomic,strong) NSMutableArray *data;
@end
// 父类控制器.m文件
/** 懒加载 */
- (NSMutableArray *)data
{
if (_data == nil) {
_data = [NSMutableArray array];
}
return _data;
}
// 初始化方法,初始化成group样式的tableView
- (id)init
{
return [self initWithStyle:UITableViewStyleGrouped];
}
// 初始化方法,父类选tableViewController,系统默认生成
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
// 父类控制器的数据源,代理方法
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 每组有多少行,取决于items.count
QKGroup *group = self.data[section];
return group.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 自定义UITableViewCell的三部曲,虽然还没创建cell,可以先写好
// 初始化自定义cell
QKSetCell *cell = [QKSetCell cellWithTableView:tableView];
// 传模型
QKGroup *group = self.data[indexPath.section];
cell.item = group.items[indexPath.row];
// 返回
return cell ;
}
#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
QKGroup *group = self.data[indexPath.section];
QKBaseItem *item = group.items[indexPath.row];
if ([item isKindOfClass:[QKArrowsItem class]]) { // 带箭头的item
QKArrowsItem *arrowsItem = (QKArrowsItem *)item;
[self.navigationController pushViewController:[[arrowsItem.destVC alloc]init] animated:YES];
}
}
2.2.1建子类控制器
// 图1主设置界面的控制器
- (void)viewDidLoad
{
[super viewDidLoad];
QKGroup *group = [[QKGroup alloc]init];
QKArrowsItem *inform = [QKArrowsItem itemWithTitle:@"推送和提醒" Icon:@"MorePush" desc:[QKPushNoticeController class]];
group.items = @[inform];
[self.data addObject:group];
}
2.3建view--自定义tableViewCell
// 不光自定义cell,其他自定义的view通常都会包含模型属性,这样才能通过set方法,去设置数据,frame等
@property (nonatomic,strong) QKBaseItem *item;
// 自定义cell通常都会有下面这个方法
+ (instancetype) cellWithTableView:(UITableView *)tableView;
// cell的初始化方法
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"set";
QKSetCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[QKSetCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
}
- (void)setItem:(QKBaseItem *)item
{
_item = item;
// 设置icon,title,控件的frame
[self setupData];
// 设置箭头,switch,label,需要判断item的类,isKindOfClass
[self setupRightContent];
}