仿照UITableView的UITableViewDataSource 协义
// 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) IBOutlet UITableView *leftTV;
@property (weak, nonatomic) IBOutlet UITableView *rightTV;
@property (nonatomic,assign) NSInteger selectRow;
@end
@implementation popView
+ (popView *)makePopView
{
return [[[NSBundle mainBundle]loadNibNamed:@"popView" owner:self options:nil]firstObject];
}
#pragma mark - tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == _leftTV) {
return [self.dataSource numberOfRowsInLeftTable:self];
}else{
return [self.dataSource popView:self subDataForRow:_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 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = [self.dataSource popView:self titleForRow:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.dataSource popView:self imageForRow:indexPath.row]];
NSArray *subDataArray = [self.dataSource popView:self subDataForRow: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 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.textLabel.text = [self.dataSource popView:self subDataForRow:_selectRow][indexPath.row];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == _leftTV) {
self.selectRow = indexPath.row;
[_rightTV reloadData];
}
}
@end
在控制器使用
#import "PopViewController.h"
#import "popView.h"
#import "CategoriyModel.h"
@interface PopViewController ()<MyPopviewDataSource>
@end
@implementation PopViewController
- (void)viewDidLoad
{
[super viewDidLoad];
popView *pop = [popView makePopView];
[self.view addSubview:pop];
pop.dataSource = self;
pop.autoresizingMask = UIViewAutoresizingNone;
self.preferredContentSize = CGSizeMake(pop.frame.size.width, pop.frame.size.height);
}
//获取到 第一个分类数据下拉菜单的模型数组
- (NSArray *)getData
{
CategoriyModel *md = [[CategoriyModel alloc]init];
NSArray *categorieyArray = [md loadPlistData];
return categorieyArray;
}
#pragma mark - popview dataSource
- (NSInteger)numberOfRowsInLeftTable:(popView *)popView{
return [self getData].count;
}
- (NSString *)popView:(popView *)popView titleForRow:(NSInteger)row{
return [[self getData][row]name];
}
- (NSString *)popView:(popView *)popView imageForRow:(NSInteger)row{
return [[self getData][row]small_icon];
}
- (NSArray *)popView:(popView *)popView subDataForRow:(NSInteger)row{
return [[self getData][row]subcategories];
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/baitxaps/article/details/47091923