//下拉列表
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