iOS開發_iphone開發_iphone介面如何?下拉式清單

來源:互聯網
上載者:User

代碼如下:
    
    #import <UIKit/UIKit.h>
    @interface DropDownList : UIView<UITableViewDelegate,UITableViewDataSource> {
    UITextField* textField;   //文本輸入框
    NSArray* list;            //下拉式清單資料
    BOOL showList;             //是否彈出下拉式清單
    UITableView* listView;    //下拉式清單
    CGRect oldFrame,newFrame;   //整個控制項(包括下拉前和下拉後)的矩形
    UIColor *lineColor,*listBgColor;//下拉框的邊框色、背景色
    CGFloat lineWidth;               //下拉框邊框粗細
    UITextBorderStyle borderStyle;   //文字框邊框style
    }
    @property (nonatomic,retain)UITextField *textField;
    @property (nonatomic,retain)NSArray* list;
    @property (nonatomic,retain)UITableView* listView;
    @property (nonatomic,retain)UIColor *lineColor,*listBgColor;
    @property (nonatomic,assign)UITextBorderStyle borderStyle;
    -(void)drawView;
    -(void)setShowList:(BOOL)b;
    @end
    #import "DropDownList.h"
    @implementation DropDownList
    @synthesize textField,list,listView,lineColor,listBgColor,borderStyle;
    - (id)initWithFrame:(CGRect)frame {
    
    if(self=[super initWithFrame:frame]){
    //預設的下拉式清單中的資料
    list=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
    
    borderStyle=UITextBorderStyleRoundedRect;
    
    showList=NO; //預設不顯示下拉框
    oldFrame=frame; //未下拉時控制項初始大小
    //當下拉框顯示時,計算出控制項的大小。
    newFrame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height*5);
    
    lineColor=[UIColor lightGrayColor];//預設列表邊框線為灰色
    listBgColor=[UIColor whiteColor];//預設列表框背景色為白色
    lineWidth=1;     //預設列表邊框粗細為1
    
    //把背景色設定為透明色,否則會有一個黑色的邊
    self.backgroundColor=[UIColor clearColor];
    [self drawView];//調用方法,繪製控制項
    
    }
    returnself;
    }
    -(void)drawView{
    //文字框
    textField=[[UITextField alloc]
      initWithFrame:CGRectMake(0, 0,
    oldFrame.size.width, 
    oldFrame.size.height)];
    textField.borderStyle=borderStyle;//設定文字框的邊框風格
    [self addSubview:textField];
        [textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents]; 
    
    //下拉式清單
    listView=[[UITableView alloc]initWithFrame:
      CGRectMake(lineWidth,oldFrame.size.height+lineWidth, 
    oldFrame.size.width-lineWidth*2,
    oldFrame.size.height*4-lineWidth*2)];
    listView.dataSource=self;
    listView.delegate=self;
    listView.backgroundColor=listBgColor;
    listView.separatorColor=lineColor;
    listView.hidden=!showList;//一開始listView是隱藏的,此後根據showList的值顯示或隱藏
    
    [self addSubview:listView]; 
    [listView release];
    }
    -(void)dropdown{
    [textField resignFirstResponder];
    if (showList) {//如果下拉框已顯示,什麼都不做
    return;
    }else {//如果下拉框尚未顯示,則進行顯示
    //把dropdownList放到前面,防止下拉框被別的控制項遮住
    
    [self.superview bringSubviewToFront:self];
    [self setShowList:YES];//顯示下拉框
    }
    }
    #pragma mark listViewdataSource method and delegate method
    -(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
    return list.count;
    }
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellid=@"listviewid";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellid];
    if(cell==nil){
    cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
      reuseIdentifier:cellid]autorelease];
    }
    //文字標籤
    cell.textLabel.text=(NSString*)[list objectAtIndex:indexPath.row];
    cell.textLabel.font=textField.font;
    
    cell.selectionStyle=UITableViewCellSelectionStyleGray;
    return cell;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return oldFrame.size.height;
    }
    //當選擇下拉式清單中的一行時,設定文字框中的值,隱藏下拉式清單
    -(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSLog(@"select");
    textField.text=(NSString*)[list objectAtIndex:indexPath.row];
    //NSLog(@"textField.text=%@",textField.text);
    [self setShowList:NO];
    }
    -(BOOL)showList{//setShowList:No為隱藏,setShowList:Yes為顯示
    return showList;
    }
    -(void)setShowList:(BOOL)b{
    showList=b;
    NSLog(@"showlist is set ");
    if(showList){
    self.frame=newFrame;
    }else {
    self.frame=oldFrame;
    }
    listView.hidden=!b;
    }
    /*
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code.
    }
    */
    - (void)dealloc {
        [super dealloc];
    }
    @end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.