一.思路:
1.使用UITableViewController来展示数据
2.通过delegate来传递数据
3.在所在页面加载UITableViewController添加UITableViewController.view
二.实现:
1. UITableViewController:
DropDownMenuTableViewController.h:
#import <UIKit/UIKit.h> @protocol DropDownMenuDelegate <NSObject> /** * 选中cell的代理事件 */ - (void) selectedCell:(NSInteger)index; /** * 更新下拉菜单的高度 */ //- (void) updateListH; @end @interface DropDownMenuTableViewController : UITableViewController @property (nonatomic) BOOL isOpen; @property (nonatomic) NSArray *dataSource; @property (nonatomic,weak) id<DropDownMenuDelegate>dropDownMenuDelegate; @end
DropDownMenuTableViewController.m:
#import "DropDownMenuTableViewController.h" @interface DropDownMenuTableViewController () @end @implementation DropDownMenuTableViewController - (void)viewDidLoad { [super viewDidLoad]; // 背景透明 self.view.backgroundColor = [UIColor whiteColor]; // 清除多余的分割线 [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]]; [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // 默认关闭下拉列表 _isOpen = NO; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 展开与隐藏账号列表 if(_isOpen) return _dataSource.count; else return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *specialId = @"id"; UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:specialId]; // 添加数据源 cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row]; cell.textLabel.textAlignment = NSTextAlignmentCenter; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //通知代理 [_dropDownMenuDelegate selectedCell:indexPath.row]; } @end
2.ViewController:
@interface MainViewController ()<GridTableDelegate>
@property (strong, nonatomic) IBOutlet UITextField *encryption_text;
@property (nonatomic) NSArray *encryption_dataSource; @property (nonatomic) DropDownMenuTableViewController *encrypt_dropDownMenuTable;
自己准备数据源
手动初始化下拉栏view:
-(void)initView{ //set text field not editable _encryption_text.enabled = false; _encrypt_dropDownMenuTable = [[DropDownMenuTableViewController alloc]init]; // 设置弹出菜单的代理为当前这个类 _encrypt_dropDownMenuTable.dropDownMenuDelegate = self; // 数据传给下拉列表类,作为表格的数据源 _encrypt_dropDownMenuTable.dataSource = _encryption_dataSource; // 将下拉列表作为子页面添加到当前视图,同时添加子控制器 [self addChildViewController:_encrypt_dropDownMenuTable]; // 根据显示框尺寸设置弹出菜单的位置和尺寸 CGFloat encryptTv_x = _encryption_text.frame.origin.x; CGFloat encryptTv_y = _encryption_text.frame.origin.y; CGFloat encryptTv_width = _encryption_text.frame.size.width; CGFloat encryptTv_height = _encryption_text.frame.size.height; _encrypt_dropDownMenuTable.view.frame = CGRectMake(encryptTv_x, encryptTv_y + encryptTv_height, encryptTv_width, 0.15*screen_height); }
点击按钮后弹出下拉框:
- (IBAction)encryptDropDownMenuBtnClicked:(UIButton *)sender { NSLog(@"encrypt drop down menu isOpen:%d",_encrypt_dropDownMenuTable.isOpen); if (_encrypt_dropDownMenuTable.isOpen) { [_encrypt_dropDownMenuTable.view removeFromSuperview]; }else{ [_pop_view addSubview:_encrypt_dropDownMenuTable.view]; [_channel_dropDownMenuTable.view removeFromSuperview]; } _encrypt_dropDownMenuTable.isOpen = !_encrypt_dropDownMenuTable.isOpen; [_encrypt_dropDownMenuTable.tableView reloadData]; //处理滚动条 [_encrypt_dropDownMenuTable.tableView performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:0]; }
*注意要remove掉tableview的view,因为即使counts变为0,也会遮挡了下面的控件;
设置table点击事件:
/** * 选定cell获取选中账号的代理监听 */ - (void)selectedCell:(NSInteger)index { // 更新当前选中账号 _encryption_text.text = [_encryption_dataSource objectAtIndex:index]; //close table view _encrypt_dropDownMenuTable.isOpen = NO; [_encrypt_dropDownMenuTable.tableView reloadData]; [_encrypt_dropDownMenuTable.view removeFromSuperview]; }
参考:
1.http://blog.csdn.net/cordova/article/details/51607142