当前位置: 首页 > 工具软件 > PopView > 使用案例 >

iPad popView封装

栾瑞
2023-12-01


仿照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


 类似资料: