在 UISearchbar 和 UITextfields 下方添加下拉建议

编程入门 行业动态 更新时间:2024-10-22 07:40:08
本文介绍了在 UISearchbar 和 UITextfields 下方添加下拉建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的 ios 应用程序中,有 3 个单独的文本输入字段.一个在 Searchbar 中,两个在 UIAlertview(有两个输入字段)中.我需要在可以向用户提供建议的字段下方提供一个下拉菜单.我有数组中所需的数据(所有 3 个字段都需要相同的数组以供建议).我该怎么做?我应该以编程方式创建一个 UITableView,它会出现在所有 TextFields 下方(带有最基本的单元格,包含一个文本字段)?如果是这样,我怎样才能获得正确的框架?或者,我应该参考什么其他方法?

In my ios app, there are 3 separate text entry fields. One in a Searchbar and two in a UIAlertview(that has two entry fields). I need to give a drop down menu below the fields that can give the suggestions to the user. I have the data needed in an array(all 3 fields need the same array for suggestion). How would I do this? Should I programmatically create a UITableView that would appear below all the TextFields(with the most basic cell, containing one text field) ? If So, how can I get the right frame? Or else, what other method should I refer?

推荐答案

这是一个简单的例子.这里我拿了一个 UITextField.当它成为第一响应者时,我会显示下拉菜单,即 UITableView .当从表视图中选择一个对象时,下拉菜单将折叠.

Here is a simple example. Here i am taking one UITextField. When it becomes first responder, i am showing the drop down i.e., UITableView . When an object selected from the table view the drop down will collapse.

- (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"); }]; }

更多推荐

在 UISearchbar 和 UITextfields 下方添加下拉建议

本文发布于:2023-06-07 21:58:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568503.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建议   UISearchbar   UITextfields

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!