iphone界面如何实现下拉列表2

本文介绍了一种在 iOS 应用中实现下拉列表的方法,包括 UITableView 的初始化配置、数据源及代理方法的实现,以及如何响应用户选择并更新 UI。
  //下拉列表
    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;
    }
    /*
    - (void)drawRect:(CGRect)rect {
        // Drawing code.
    }
    */
    - (
void)dealloc {
        [
super
 dealloc];
    }
    @end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值