【问题标题】:Swift 1/2 AutoComplete field case insensitiveSwift 1/2 AutoComplete 字段不区分大小写
【发布时间】:2015-10-09 21:28:21
【问题描述】:

我有一个代码,它使用 json 检索一个数组,其中所有名称都存储在数据库中,当您在 textField 中键入时,您可以看到建议和自动完成功能。一切都很好,唯一的问题是区分大小写,事实上,如果我写“你好”它什么也找不到,相反,如果我输入“你好”他建议标签..我该如何解决这个问题?非常感谢你。我没有找到任何相关的教程。

这是我的代码:

class ViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate,UITableViewDataSource, UITableViewDelegate {

let save = NSUserDefaults.standardUserDefaults()

@IBOutlet var amountPoints: UILabel!
@IBOutlet var reasonView: UITextView!
@IBOutlet var toField: UITextField!
@IBOutlet var pointsField: UITextField!

@IBOutlet var autocompleteTableView: UITableView!
var pastUrls: [String] = []
var autocompleteUrls = [String]()
override  func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    toField.delegate = self
    reasonView.layer.cornerRadius = 1
    reasonView.layer.borderWidth = 0.7
    reasonView.layer.borderColor = UIColor.grayColor().CGColor
    autocompleteTableView.delegate = self
    autocompleteTableView.dataSource = self
    autocompleteTableView.scrollEnabled = true
    autocompleteTableView.hidden = true

    getallUser()
    var Names = save.arrayForKey("give.Name")
    pastUrls = Names as! [String]
    print(pastUrls)
}
    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    autocompleteTableView.hidden = false
    let substring = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)

    searchAutocompleteEntriesWithSubstring(substring)
    return true     // not sure about this - could be false
}

func searchAutocompleteEntriesWithSubstring(substring: String)
{
    autocompleteUrls.removeAll(keepCapacity: false)

    for curString in pastUrls
    {
        let myString:NSString! = curString as NSString

        let substringRange :NSRange! = myString.rangeOfString(substring)

        if (substringRange.location  == 0)
        {
            autocompleteUrls.append(curString)
        }
    }

    autocompleteTableView.reloadData()
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return autocompleteUrls.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier, forIndexPath: indexPath) as UITableViewCell
    let index = indexPath.row as Int

    cell.textLabel!.text = autocompleteUrls[index]
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    toField.text = selectedCell.textLabel!.text
    autocompleteTableView.hidden = true
}








func textViewDidBeginEditing(textView: UITextView) {
    reasonView.text = ""
}

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if text == "\n"
    {
        textView.resignFirstResponder()
        return false
    }
    return true
}



@IBAction func giveButton(sender: UIButton) {


}

@IBAction func returnButton(sender: UIBarButtonItem) {
    self.dismissViewControllerAnimated(true, completion: nil)

}

}

【问题讨论】:

    标签: arrays swift uitableview uitextfield swift2


    【解决方案1】:

    首先,如果您只是想进行前缀搜索,您可以使用hasPrefix: 方法。使用此方法而不考虑大小写的一种简单方法是在调用比较方法之前获取每个字符串的小写版本:

    if (myString.lowercaseString.hasPrefix(substring.lowercaseString))
    {
        autocompleteUrls.append(curString)
    }
    

    如果您不介意切换回NSString 类型,也可以使用rangeOfString:options: 方法:

    let range = (myString as NSString).rangeOfString(substring, options:[.CaseInsensitiveSearch])
    if range.location == 0 {
        autocompleteUrls.append(curString)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2016-11-12
      • 2015-10-21
      • 2023-03-23
      • 2020-08-29
      相关资源
      最近更新 更多