Ios 键入时显示下拉菜单

Ios 键入时显示下拉菜单,ios,uitableview,uiscrollview,uitextfield,Ios,Uitableview,Uiscrollview,Uitextfield,我有一个UITextField,我想创建一些类似于ios联系人应用程序中的下拉菜单的东西 当用户开始输入时,我希望菜单下拉。它实际上不需要根据名称来限制显示的数据,但如果可以的话,那就太好了(例如:如果用户键入“m”,它只显示以m开头的字符串,以此类推)。如果用户选择一个,则该类型将发送到UITextField,并在其中显示。如果有这样的开源选择器,那就太好了。如果没有,是否有一种方法来表示包含数组中所有项的内容。这不必太复杂,也不必限制用户键入或任何内容时显示的数据。基于链接的响应。将向您展示

我有一个UITextField,我想创建一些类似于ios联系人应用程序中的下拉菜单的东西

当用户开始输入时,我希望菜单下拉。它实际上不需要根据名称来限制显示的数据,但如果可以的话,那就太好了(例如:如果用户键入“m”,它只显示以m开头的字符串,以此类推)。如果用户选择一个,则该类型将发送到UITextField,并在其中显示。如果有这样的开源选择器,那就太好了。如果没有,是否有一种方法来表示包含数组中所有项的内容。这不必太复杂,也不必限制用户键入或任何内容时显示的数据。

基于链接的响应。将向您展示如何在自动完成的下拉列表中获取URL值

  • 您需要一个具有可能的自动完成值的NSMutableArray。在本例中,我们将使用PastURL的NSMutableArray,每次用户浏览URL时,我们都会将其添加到数组中
  • 您需要创建一个UITable来显示这些值

     autocompleteTableView = [[UITableView alloc] initWithFrame:
     CGRectMake(0, 80, 320, 120) style:UITableViewStylePlain];
     autocompleteTableView.delegate = self;
     autocompleteTableView.dataSource = self;
     autocompleteTableView.scrollEnabled = YES;
     autocompleteTableView.hidden = YES;  
     [self.view addSubview:autocompleteTableView];
    
  • 编辑字段时,需要显示表格

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
         autocompleteTableView.hidden = NO;
    
         NSString *substring = [NSString stringWithString:textField.text];
         substring = [substring stringByReplacingCharactersInRange:range withString:string];
         [self searchAutocompleteEntriesWithSubstring:substring];
         return YES;
    }
    
     -(void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
    
         // Put anything that starts with this substring into the autocompleteUrls array
    
         // The items in this array is what will show up in the table view
    
         [autocompleteUrls removeAllObjects];
    
         for(NSString *curString in pastUrls)  {
              NSRange substringRange = [curString rangeOfString:substring];
              if (substringRange.location == 0) {
                   [autocompleteUrls addObject:curString]; 
              }
         }
              [autocompleteTableView reloadData];
      }
    
  • 最后只显示正在编辑的表中的内容

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
         autocompleteTableView.hidden = NO;
    
         NSString *substring = [NSString stringWithString:textField.text];
         substring = [substring stringByReplacingCharactersInRange:range withString:string];
         [self searchAutocompleteEntriesWithSubstring:substring];
         return YES;
    }
    
     -(void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
    
         // Put anything that starts with this substring into the autocompleteUrls array
    
         // The items in this array is what will show up in the table view
    
         [autocompleteUrls removeAllObjects];
    
         for(NSString *curString in pastUrls)  {
              NSRange substringRange = [curString rangeOfString:substring];
              if (substringRange.location == 0) {
                   [autocompleteUrls addObject:curString]; 
              }
         }
              [autocompleteTableView reloadData];
      }
    

  • 不要忘记将适当的UITable和UITextfield委托添加到.h文件中

    这是一个链接只回答兄弟。你会很快被选中的。我建议你扩展一下。我没意识到链接只是不受欢迎。我会放更多的信息。+1是一个很好的答案。:)哈哈,谢谢。你只需在文本字段下方添加一个表格和滚动视图(以显示任意数量的项目),并将其隐藏,当你开始键入时,使其可见。这是你唯一需要做的。对于筛选人员,你可以使用NSPredicate。如果你需要一些指导,有许多下拉列表示例。看看。。