這是一個簡單的例子。這裏我帶着一個UITextField。當它成爲第一響應者時,我顯示下拉即UITableView。當從表格中選擇一個對象時,下拉菜單將會崩潰。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mutArr=[[NSMutableArray alloc]init];
[mutArr addObject:@"object1"];
[mutArr addObject:@"object2"];
[mutArr addObject:@"object3"];
[mutArr addObject:@"object4"];
[mutArr addObject:@"object5"];
[mutArr addObject:@"object6"];
[mutArr addObject:@"object7"];
[mutArr addObject:@"object8"];
txtList1=[[UITextField alloc]init];
[txtList1 setFrame:CGRectMake(30, 100, 260, 30)];
[txtList1 setTag:100];
[txtList1 setDelegate:self];
[txtList1 setBackgroundColor:[UIColor brownColor]];
[txtList1 setTextColor:[UIColor whiteColor]];
[self.view addSubview:txtList1];
tblList=[[UITableView alloc]init];
[tblList setDelegate:self];
[tblList setDataSource:self];
[self.view addSubview:tblList];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mutArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[[UITableViewCell alloc]init];
[cell setBackgroundColor:[UIColor grayColor]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.text=[mutArr objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tblList cellForRowAtIndexPath:indexPath];
txtList1.text=cell.textLabel.text;
[txtList1 resignFirstResponder];
[self collapseTableView];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
if (textField.tag==100)
{
CGRect rect=tblList.frame;
if (rect.size.height==0)
{
[self expandTableView];
}
}
}
-(void)collapseTableView
{
[tblList setFrame:CGRectMake(30, 130, 260, 120)];
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
[tblList setFrame:CGRectMake(30, 130, 260, 0)];
}
completion:^(BOOL finished)
{
NSLog(@"comleted");
}];
}
-(void)expandTableView
{
[tblList setFrame:CGRectMake(30, 130, 260, 0)];
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone
animations:^{
[tblList setFrame:CGRectMake(30, 130, 260, 200)];
}
completion:^(BOOL finished)
{
NSLog(@"comleted");
}];
}
的一個主要問題仍然是對內部UIAlertView中me..the的UITextField不調用的方法 - (空)textFieldDidBeginEditing:(*的UITextField)文本框 –
你需要顯示UIAlertView中的每一件事情..?你如何將textFields添加到AlertView .. ?? – user4261201