彩票14——设置3_UITableViewController基类提取
一、说明
我们希望达到的效果是子类只需要提供数据就行了,父类完成全部的显示功能。
那么我们的父类就需要一个存储数据的属性。
其次我们看到不同的列表它的cell样式不同,所以我们还需要提供一个设置cell的方法。
二、QWMBaseTableViewController
2.1 QWMBaseTableViewController.h
//
// QWMBaseTableViewController.h
// 03_UIView79_彩票
//
// Created by 杞文明 on 17/8/30.
// Copyright © 2017年 杞文明. All rights reserved.
// UITableViewController 基类
#import <UIKit/UIKit.h>
#import "QWMSettingTableViewCell.h"
@interface QWMBaseTableViewController : UITableViewController
/** 数组总数 */
@property (nonatomic, strong) NSMutableArray *groups;
@property(nonatomic,assign)UITableViewCellStyle cellStyle;
@end
2.2 QWMBaseTableViewController.m
//
// QWMBaseTableViewController.m
// 03_UIView79_彩票
//
// Created by 杞文明 on 17/8/30.
// Copyright © 2017年 杞文明. All rights reserved.
//
#import "QWMBaseTableViewController.h"
@interface QWMBaseTableViewController ()
@end
@implementation QWMBaseTableViewController
//懒加载组
-(NSMutableArray *)groups{
if(!_groups){
_groups = [NSMutableArray array];
}
return _groups;
}
//重写init方法
-(instancetype)init{
return [super initWithStyle:UITableViewStyleGrouped];
}
-(UITableViewCellStyle)cellStyle{
return UITableViewCellStyleValue1;
}
//组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.groups.count;
}
//每组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
QWMSettingGroup *group = self.groups[section];
return group.items.count;
}
//创建每个item
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.创建cell
QWMSettingTableViewCell *cell = [QWMSettingTableViewCell cellWithTableView:tableView cellStyle:[self cellStyle]];
//2.设置数据
//取出组数据
QWMSettingGroup *group = self.groups[indexPath.section];
//取出行数据
QWMSettingItem *item = group.items[indexPath.row];
cell.item = item;
return cell;
}
//点击条目的时候操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//1.取出组模型
QWMSettingGroup *group = self.groups[indexPath.section];
//2.取出行模型
QWMSettingItem *item = group.items[indexPath.row];
//3.判断行为,做事情或者跳转
if(item.operationBlock){//如果添加了做事情,那么就开始做事情
item.operationBlock(indexPath);
}else if( [item isKindOfClass:[QWMSettingArrowItem class]] ){
//箭头那是跳转
QWMSettingArrowItem *arrowItem = (QWMSettingArrowItem*)item;
if(arrowItem.desVc){
// 有目标控制器才跳转
UIViewController *vc = [[arrowItem.desVc alloc]init];
vc.title = item.title;
[self.navigationController pushViewController:vc animated:YES];
}
}
}
//头部标题设置
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//取出组模型
QWMSettingGroup *group = self.groups[section];
return group.headerTitle;
}
//尾部标题设置
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//取出组模型
QWMSettingGroup *group = self.groups[section];
return group.footTitle;
}
@end