2015-10-12 59 views
0

我有一個文本字段,當您鍵入一個名稱時,應該顯示一個建議的名稱,從JSON數組中獲取I,問題是它只顯示一個名稱。例如,如果我鍵入Tom,它只顯示Tom Cruise,而不顯示Tommy Gien。我該如何解決這個問題?Swift 1/2 TextField標記顯示所有名稱

CODE:

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) 
    } 

回答

0

嘗試更換你的方法在seachAutocompleteEntriesWithSubtring具有以下

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

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

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

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

    autocompleteTableView.reloadData() 
} 
+0

NOP,仍然只顯示一個現在是也區分大小寫 – Marcoc