ios - 在 UISearchbar 和 UITextfields 下面添加一个下拉建议

标签 ios xcode uitableview uiview uisearchbar

在我的 iOS 应用程序中,有 3 个单独的文本输入字段。一个在搜索栏中,两个在 UIAlertview 中(有两个输入字段)。我需要在可以向用户提供建议的字段下方提供一个下拉菜单。我有一个数组中需要的数据(所有 3 个字段都需要相同的数组来提供建议)。我该怎么做?我是否应该以编程方式创建一个显示在所有 TextFields 下方的 UITableView(具有最基本的单元格,包含一个文本字段)?如果是这样,我怎样才能得到正确的框架?否则,我应该引用其他什么方法?

最佳答案

这是一个简单的例子。我在这里使用一个 UITextField。当它成为第一响应者时,我会显示下拉列表,即 UITableView 。当从 TableView 中选择一个对象时,下拉列表将折叠。

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

}

关于ios - 在 UISearchbar 和 UITextfields 下面添加一个下拉建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29814688/

相关文章:

Android、iPhone、iPad、Windows Phone 健身应用的定期(订阅)付款

ios - iOS 上的 2D 游戏

ios - Storyboard 中的错误

ios - 纵向和横向对齐两个按钮

ios - 在 UITableViewCell 中添加 AVPlayer 或 MPMoviePlayerController 的最佳位置是什么?

iOS:无效更新:第 0 节中的行数无效

ios - 在 iOS 中更改图像的打印尺寸

ios - 需要从open api获取国家名称

ios - 在 iOS 中创建一个导出的方法库类

ios - 在 UISearchDisplayController 中检测对 "no displayed search results"的点击