ios - 自动建议在UITextfield中使用逗号分隔

标签 ios nsstring uitextfield

下面的代码让我通过将其与以前添加的字符串对象数组进行比较并在UITableview中显示它,来自动建议键入到UITextfield中的值。
这很好用,但仅适用于单个单词。

所以现在,我可以用这样的方式修改代码,即在用户输入逗号之后,再次开始输入时,我可以再次搜索相同的字符串数组以获取建议,以查找在逗号之后输入的字符的建议吗?

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{

if (textField.tag == tagTextFieldTag)    //The text field where user types
{
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    autocompleteTableView.hidden = NO;   //The table which displays the autosuggest

        NSString *substring = [NSString stringWithString:textField.text];

        substring = [substring stringByReplacingCharactersInRange:range withString:string]; 

if ([substring isEqualToString:@""]) 
    {
        autocompleteTableView.hidden = YES; //hide the autosuggest table if textfield is empty
    }
        [self searchAutocompleteEntriesWithSubstring:substring];  //The method that compares the typed values with the pre-loaded string array

    }


return YES;
}

 - (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

[autoCompleteTags removeAllObjects];

for(Tag *tag3 in tagListArray)   //tagListArray contains the array of strings

 //tag3 is an object of Tag class, which has a single attribute called 'text'

{
    NSString *currentString = tag3.text;
    NSRange substringRange = [currentString rangeOfString:substring];
    if(substringRange.location ==0)
    {
        [autoCompleteTags addObject:currentString];
    }
}

[autocompleteTableView reloadData];
}

最佳答案

你可以这样

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{

  if (textField.tag == tagTextFieldTag)    //The text field where user types
  {
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    autocompleteTableView.hidden = NO;   //The table which displays the autosuggest
    NSArray *autoComplete = [textField.text componentsSeparatedByString:@","];
    NSString *substring = [NSString stringWithString:[autoComplete lastObject]];
   if ([substring isEqualToString:@""]) 
    {
        autocompleteTableView.hidden = YES; //hide the autosuggest table if textfield is empty
    }
        [self searchAutocompleteEntriesWithSubstring:substring];

    }

 return YES;
}

这种方法的工作原理是,
  • componentsSeparatedByString分隔textFields文本,并将其作为NSArray
  • lastObject从该数组中获取最后一个对象。如果它是@“”,则不进行搜索,否则搜索匹配的元素。

  • 希望这会帮助你。

    关于ios - 自动建议在UITextfield中使用逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11430293/

    相关文章:

    iphone - 如何使用 UICollectionView 复制 iOS Chrome 选项卡效果

    iphone - 从 NSString 中读取指定位置的 UTF8 字符

    ios - UITextField 子类 - 订阅事件

    ios - 如何改进 Swift/Xcode iOS 应用程序中文本字段的控制语句?

    ios - Swift:未识别 UITextField

    ios - 应用程序在使用 NewRelic 框架的设备上启动时崩溃

    ios - 使用 If-Let 并在一行中检查输出

    ios - Swift-使用委托(delegate)在 tableView 上显示

    xcode - 使用 ComponentsJoinedByString 打印数组 : method

    ios - 如何从 NSString 获取特定字节长度的子字符串