彩票14设置3_UITableViewController基类提取
一说明二QWMBaseTableViewController
1 QWMBaseTableViewControllerh2 QWMBaseTableViewControllerm
彩票14——设置3_UITableViewController基类提取
一、说明
我们希望达到的效果是子类只需要提供数据就行了,父类完成全部的显示功能。
那么我们的父类就需要一个存储数据的属性。
其次我们看到不同的列表它的cell样式不同,所以我们还需要提供一个设置cell的方法。
二、QWMBaseTableViewController
2.1 QWMBaseTableViewController.h
#
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
#import "QWMBaseTableViewController.h"
@interface QWMBaseTableViewController ()
@end
@implementation QWMBaseTableViewController
-(
NSMutableArray *)groups{
if(!_groups){
_groups = [
NSMutableArray array];
}
return _groups;
}
-(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;
}
-(
UITableViewCell*)tableView:(
UITableView *)tableView cellForRowAtIndexPath:(
NSIndexPath *)indexPath{
QWMSettingTableViewCell *cell = [QWMSettingTableViewCell cellWithTableView:tableView cellStyle:[
self cellStyle]];
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];
QWMSettingGroup *group =
self.groups[indexPath
.section];
QWMSettingItem *item = group
.items[indexPath
.row];
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