1. Ideas:
1. Use UITableViewController to display data
2. Pass data through delegate
3. Load UITableViewController on the page and add UITableViewController.view
2. Realize:
1. UITableViewController:
DropDownMenuTableViewController.h:
#import <UIKit/UIKit.h> @protocol DropDownMenuDelegate <NSObject> /** * Selected cell's proxy event */ - (void) selectedCell:(NSInteger)index; /** * Update the height of the dropdown menu */ //- (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]; // background transparent self.view.backgroundColor = [UIColor whiteColor]; // remove redundant dividing lines [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]]; [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; // close the dropdown list by default _isOpen = NO; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Expand and hide the account list 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]; // add data source cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row]; cell.textLabel.textAlignment = NSTextAlignmentCenter; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //Notify agent [_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;
Prepare the data source yourself
Manually initialize the drop-down view:
-(void)initView{ //set text field not editable _encryption_text.enabled = false; _encrypt_dropDownMenuTable = [[DropDownMenuTableViewController alloc]init]; // Set the proxy of the popup menu to the current class _encrypt_dropDownMenuTable.dropDownMenuDelegate = self; // The data is passed to the drop-down list class as the data source of the table _encrypt_dropDownMenuTable.dataSource = _encryption_dataSource; // Add the dropdown list as a subpage to the current view, and add a subcontroller at the same time [self addChildViewController:_encrypt_dropDownMenuTable]; // Set the position and size of the popup menu according to the size of the display box 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); }
After clicking the button, a drop-down box will pop up:
- (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]; // handle scrollbars [_encrypt_dropDownMenuTable.tableView performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:0]; }
*Be careful to remove the view of the tableview, because even if the counts becomes 0, it will block the following controls;
Set the table click event:
/** * The selected cell gets the proxy monitoring of the selected account */ - (void)selectedCell:(NSInteger)index { // Update the currently selected account _encryption_text.text = [_encryption_dataSource objectAtIndex:index]; //close table view _encrypt_dropDownMenuTable.isOpen = NO; [_encrypt_dropDownMenuTable.tableView reloadData]; [_encrypt_dropDownMenuTable.view removeFromSuperview]; }
refer to:
1.http://blog.csdn.net/cordova/article/details/51607142