1. 程式人生 > >IOS開發之下拉列表控制元件開發

IOS開發之下拉列表控制元件開發

效果

//  Commbox.h  
//  CommboxDemo  
//  
//  Created by xuqiang on 13-9-27.  
//  Copyright (c) 2013年 xuqiang. All rights reserved.  
//  
  
#import <UIKit/UIKit.h>  
  
@interface Commbox : UIView<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>  
{  
    UITableView *tv;//下拉列表  
    NSMutableArray *tableArray;//下拉列表資料  
    UITextField *textField;//文字輸入框  
    BOOL showList;//是否彈出下拉列表  
    CGFloat tabheight;//table下拉列表的高度  
    CGFloat frameHeight;//frame的高度  
}  
  
@property (nonatomic,retain) UITableView *tv;  
@property (nonatomic,retain) NSArray *tableArray;  
@property (nonatomic,retain) UITextField *textField;  
  
@end  
</pre><br><br>  
[html] view plaincopy
<pre name="code" class="html">//  
//  Commbox.m  
//  CommboxDemo  
//  
//  Created by xuqiang on 13-9-27.  
//  Copyright (c) 2013年 xuqiang. All rights reserved.  
//  
  
#import "Commbox.h"  
  
@implementation Commbox  
@synthesize tv,tableArray,textField;  
  
- (id)initWithFrame:(CGRect)frame  
{  
      
    if (frame.size.height<200) {  
        frameHeight = 200;  
    }else{  
        frameHeight = frame.size.height;  
    }  
    tabheight = frameHeight-30;  
      
    frame.size.height = 30.0f;  
      
    self = [super initWithFrame:frame];  
    if (self) {  
        // Initialization code  
        //textField.delegate = self;  
    }  
      
    if(self){  
        showList = NO; //預設不顯示下拉框  
          
        tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width + 80 , 0)];  
        tv.delegate = self;  
        tv.dataSource = self;  
        tv.backgroundColor = [UIColor grayColor];  
        tv.separatorColor = [UIColor lightGrayColor];  
        tv.hidden = YES;  
        [self addSubview:tv];  
          
        textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width + 80, 30)];  
        textField.font = [UIFont systemFontOfSize:8.0f];  
        //textField.userInteractionEnabled = NO;  
        textField.borderStyle=UITextBorderStyleRoundedRect;//設定文字框的邊框風格  
        [textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];  
        [self addSubview:textField];  
          
    }  
    return self;  
}  
  
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {  
////    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoBoardHidden:) name:UIKeyboardWillShowNotification object:nil];  
//    return YES;  
//}  
//  
//- (void)keyBoBoardHidden:(NSNotification *)Notification{  
//    //[self.textField resignFirstResponder];  
//    return;  
//}  
  
-(void)dropdown{  
    [textField resignFirstResponder];  
    if (showList) {//如果下拉框已顯示,什麼都不做  
        return;  
    }else {//如果下拉框尚未顯示,則進行顯示  
          
        CGRect sf = self.frame;  
        sf.size.height = frameHeight;  
          
        //把dropdownList放到前面,防止下拉框被別的控制元件遮住  
        [self.superview bringSubviewToFront:self];  
        tv.hidden = NO;  
        showList = YES;//顯示下拉框  
          
        CGRect frame = tv.frame;  
        frame.size.height = 0;  
        tv.frame = frame;  
        frame.size.height = tabheight;  
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];  
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];  
        self.frame = sf;  
        tv.frame = frame;  
        [UIView commitAnimations];  
    }  
}  
  
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
{  
    return 1;  
}  
  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
{  
    return [tableArray count];  
}  
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    static NSString *CellIdentifier = @"Cell";  
      
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil) {  
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;  
    }  
      
    cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];  
    cell.textLabel.font = [UIFont systemFontOfSize:8.0f];  
    cell.accessoryType  = UITableViewCellAccessoryNone;  
    cell.selectionStyle = UITableViewCellSelectionStyleGray;  
      
    return cell;  
}  
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return 35;  
}  
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    textField.text = [tableArray objectAtIndex:[indexPath row]];  
    showList = NO;  
    tv.hidden = YES;  
      
    CGRect sf = self.frame;  
    sf.size.height = 30;  
    self.frame = sf;  
    CGRect frame = tv.frame;  
    frame.size.height = 0;  
    tv.frame = frame;  
}  
  
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
{  
    // Return YES for supported orientations  
    return (interfaceOrientation == UIInterfaceOrientationPortrait);  
}  
/*  
// Only override drawRect: if you perform custom drawing.  
// An empty implementation adversely affects performance during animation.  
- (void)drawRect:(CGRect)rect  
{  
    // Drawing code  
}  
*/  
  
@end  
</pre><pre name="code" class="html"></pre><pre name="code" class="html">下面是新增程式碼</pre><pre name="code" class="html"><pre name="code" class="html">//  
//  ViewController.m  
//  CommboxDemo  
//  
//  Created by xuqiang on 13-9-27.  
//  Copyright (c) 2013年 xuqiang. All rights reserved.  
//  
  
#import "ViewController.h"  
  
@interface ViewController ()  
  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
    //*****************************************************************************************************************  
    NSMutableDictionary *mulDic = [NSMutableDictionary dictionary];  
    [mulDic setObject:[NSArray arrayWithObjects:@"15000000", @"/MHz"    , nil] forKey:@"蜂窩公眾通訊(全國網)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"15000000", @"/MHz"    , nil] forKey:@"蜂窩公眾通訊(非全國網)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"50000",    @"每頻點"   , nil] forKey:@"叢集無線排程系統(全國範圍使用)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"10000",    @"每頻點"   , nil] forKey:@"叢集無線排程系統(省、自治區、直轄市範圍使用)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"2000",     @"每頻點"   , nil] forKey:@"叢集無線排程系統(地、市範圍使用)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"2000000",  @"每頻點"   , nil] forKey:@"無線尋呼系統(全國範圍使用)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"2000000",  @"每頻點"   , nil] forKey:@"無線尋呼系統(省、自治區、直轄市範圍使用"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"40000",    @"每頻點"   , nil] forKey:@"無線尋呼系統(地、市範圍使用)"];  
    [mulDic setObject:[NSArray arrayWithObjects:@"150",      @"每基站"   , nil] forKey:@"無繩電話系統"];  
   
    arrayData = [mulDic allKeys];  
    //*****************************************************************************************************************  
     
    _commbox = [[Commbox alloc] initWithFrame:CGRectMake(70, 10, 140, 100)];  
    _commbox.textField.placeholder = @"點選請選擇";  
      
    NSMutableArray* arr = [[NSMutableArray alloc] init];  
    //*****************************************************************************************************************  
    NSEnumerator *e = [mulDic keyEnumerator];  
    for (NSString *key in e) {  
        //NSLog(@"Key is %@, value is %@", key, [mulDic objectForKey:key]);  
       [arr addObject:key];  
          
    }  
    //NSLog(@"%@",[arr objectAtIndex:0]);  
    //*****************************************************************************************************************  
  
    _commbox.tableArray = arr;  
  
    [self.view addSubview:_commbox];  
   
  
}  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}