iphone界面如何实现下拉列表2
2013-01-24 15:36
453 查看
//下拉列表
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
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
相关文章推荐
- iphone界面如何实现下拉列表
- iphone界面如何实现下拉列表1
- iphone界面如何实现下拉列表
- iphone 界面如何实现下拉列表
- iOS开发_iphone开发_iphone界面如何实现下拉列表
- iOS开发_iphone开发_iphone界面如何实现下拉列表
- iPhone界面如何实现下拉列表
- iphone 界面实现下拉列表
- iphone 界面实现下拉列表
- iOS开发_iphone开发_iphone界面怎么实现下拉列表
- ExpandableListView 功能界面布局探究之三(没有child的下拉列表,group不展开的实现)
- Vue.js自定义下拉列表,如何实现在下拉列表区域外点击即可关闭下拉列表的功能
- KK上全屏界面如何实现禁止状态栏下拉?
- 如何在DataGridView中实现下拉列表可变的联动
- 如何借助SimpleAdapter和Spinner实现下拉列表
- Access-培训管理系统-04-清空界面及下拉列表实现(上篇)
- -- 如何实现圆角的UITextView --iphone短信发送(非系统界面)
- Access-培训管理系统-05-清空界面及下拉列表实现(下篇)
- 如何借助ArrayAdapter和Spinner实现下拉列表