• iPad popView封装



    仿照UITableViewUITableViewDataSource 协义

    1.代理。让代理帮我们类完毕一些方法

    2.完毕当前类不能完毕的事情还有传值等功能


    实现方法

    // 1. 声明一个协议

    // 2. 声明协议中的方法

    // 3. 声明一个遵守协议的id类型的指针

    // 4. 实现协议方法


    @class popView;

    @protocol MyPopviewDataSource <NSObject>

    //制定协议方法


    //left tablevie 行数

    - (NSInteger)numberOfRowsInLeftTable:(popView *)popView;

    //left 标题

    - (NSString *)popView:(popView *)popView titleForRow:(NSInteger)row;

    //left 图标

    - (NSString *)popView:(popView *)popView imageForRow:(NSInteger)row;

    //left 子数据

    - (NSArray *)popView:(popView *)popView subDataForRow:(NSInteger)row;

    @end


    @interface popView :UIView

    @property (nonatomic,assign)id<MyPopviewDataSource> dataSource;

    + (popView*)makePopView;

    @end



    #import "popView.h"

    @interface popView ()<UITableViewDataSource,UITableViewDelegate>

    @property (weak, nonatomic) IBOutletUITableView *leftTV;

    @property (weak, nonatomic) IBOutletUITableView *rightTV;

    @property (nonatomic,assign)NSInteger selectRow;

    @end


    @implementation popView

    + (popView *)makePopView

    {

        return [[[NSBundlemainBundle]loadNibNamed:@"popView"owner:selfoptions:nil]firstObject];

    }


    #pragma mark - tableview delegate

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        if (tableView == _leftTV) {

            return [self.dataSourcenumberOfRowsInLeftTable:self];

        }else{

            return [self.dataSourcepopView:selfsubDataForRow:_selectRow].count;

        }

    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (tableView == _leftTV) {

            static NSString *str =@"Mycell";

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

            if (cell == nil) {

                cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];

            }

            cell.textLabel.text = [self.dataSourcepopView:selftitleForRow:indexPath.row];

            cell.imageView.image = [UIImageimageNamed:[self.dataSourcepopView:selfimageForRow:indexPath.row]];

            

            NSArray *subDataArray = [self.dataSourcepopView:selfsubDataForRow:indexPath.row];

            

            if (subDataArray.count) {

                cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

            }else{

                cell.accessoryType =UITableViewCellAccessoryNone;

            }

            return cell;

            

        }else{

            static NSString *str =@"Mycell";

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

            if (cell == nil) {

                cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];

            }

            cell.textLabel.text = [self.dataSourcepopView:selfsubDataForRow:_selectRow][indexPath.row];

            return cell;

        }

    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (tableView == _leftTV) {

            self.selectRow = indexPath.row;

            [_rightTVreloadData];

        }

    }

    @end


    在控制器使用

    #import "PopViewController.h"

    #import "popView.h"

    #import "CategoriyModel.h"


    @interface PopViewController ()<MyPopviewDataSource>

    @end


    @implementation PopViewController


    - (void)viewDidLoad

    {

        [superviewDidLoad];

        popView *pop = [popViewmakePopView];

        [self.viewaddSubview:pop];

        pop.dataSource =self;

        pop.autoresizingMask =UIViewAutoresizingNone;

        self.preferredContentSize =CGSizeMake(pop.frame.size.width, pop.frame.size.height);

    }


    //获取到 第一个分类数据下拉菜单的模型数组

    - (NSArray *)getData

    {

        CategoriyModel *md = [[CategoriyModelalloc]init];

        NSArray *categorieyArray = [md loadPlistData];

        return categorieyArray;

    }



    #pragma mark - popview dataSource

    - (NSInteger)numberOfRowsInLeftTable:(popView *)popView{

        return [selfgetData].count;

    }


    - (NSString *)popView:(popView *)popView titleForRow:(NSInteger)row{

        return [[selfgetData][row]name];

    }


    - (NSString *)popView:(popView *)popView imageForRow:(NSInteger)row{

        return [[selfgetData][row]small_icon];

    }


    - (NSArray *)popView:(popView *)popView subDataForRow:(NSInteger)row{

        return [[selfgetData][row]subcategories];

    }


    @end


  • 相关阅读:
    P4932 浏览器 题解
    P1627 [CQOI2009]中位数 题解
    P4626 一道水题 II 题解
    P1439 【模板】最长公共子序列 题解
    P2324 [SCOI2005]骑士精神 题解
    P1784 数独 题解
    浅谈 Dancing Links X 算法
    P5905 【模板】Johnson 全源最短路 题解
    线性预处理阶乘,逆元和组合数
    需要支持多种操作的线段树该如何确定运算顺序?
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6973634.html
Copyright © 2020-2023  润新知