用逗号分隔在 UITextfield 中自动建议 [英] Auto suggest in UITextfield with comma separation

查看:31
本文介绍了用逗号分隔在 UITextfield 中自动建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码让我自动建议输入到 UITextfield 中的值,方法是将它与之前添加的字符串对象数组进行比较并将其显示在 UITableview 中.这工作正常,但仅适用于单个单词.

The following code lets me auto suggest values typed into a UITextfield, by comparing it to an array of previously added string objects and show it in a UITableview. This works fine, but only for a single word.

那么现在,我可以这样修改代码,在用户输入逗号后,然后再次开始输入,我可以再次搜索相同的字符串数组以获取逗号后输入的字符的建议吗?

So now, can I modify the code in such a way, that after the user enters a comma, then starts typing again, I can search the same string array for suggestions again for the characters typed after the comma?

- (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;
}

这种方法的工作原理是,

this method works like,

  • componentsSeparatedByString, 分隔 textFields 文本并将其作为 NSArray
  • lastObject 从该数组中获取最后一个对象.如果是@"" 则不搜索,否则搜索匹配元素.
  • componentsSeparatedByString seperates the textFields text by , and give it as an NSArray
  • lastObject takes the last object from that array. If it is @"" no searching otherwise searches for an matching element.

希望对您有所帮助.

这篇关于用逗号分隔在 UITextfield 中自动建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆