iOS学习笔记-099.彩票14——设置3_UITableViewController基类提取

彩票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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值