Getting autocomplete to work in swift

前端 未结 12 489
清酒与你
清酒与你 2020-12-04 15:55

I am trying to implement autocompletion, but can\'t find an example that works in Swift. Below, I\'m tring to convert Ray Wenderlich\'s autocompletion tutorial and example c

相关标签:
12条回答
  • 2020-12-04 16:41

    This post helped me thanks, just in case you guys are working with google places API in swift 3 and you need case-insensitive here is the updated code you just have to put:

    let subStringRange : NSRange! = myString.range(of: substring, options: .caseInsensitive)
    
    0 讨论(0)
  • 2020-12-04 16:41

    Replace cellForRowAtIndexPath with following function

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        var cell = UITableViewCell()
        var data = autocompleteUrls[indexPath.row]
        cell.textLabel?.text = data as String
        return cell
    }
    
    0 讨论(0)
  • 2020-12-04 16:41

    Add view.addSubview(autocompleteTableView) in your viewdidload. It will work.

    0 讨论(0)
  • 2020-12-04 16:43

    Here's a way to add multiple tags based on "#" being typed in like twitter.

    Variable typedSubstring is the global substring.

      func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
    
    autocompleteTableView!.hidden = false
    var changedText = (self.textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
    var items = changedText.componentsSeparatedByString("#")
    
    if (items.count > 0) {
      typedSubstring  = "#" + items.lastObject as NSString
    
      self.searchAutocompleteEntriesWithSubstring(typedSubstring)
    }
    
    return true
    

    }

    Improved on DrWhat's solution so that when you select a cell, it appends it correctly after where the user has already typed in.

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    
    let selectedText = selectedCell.textLabel?.text as String!
    
    // Remove what has been typed already
    let trimmedString = selectedText.stringByReplacingOccurrencesOfString(typedSubstring, withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    
    var currentTextField = textField.text
    
    // then append to what has been typed
    textField.text = currentTextField + trimmedString
    
    0 讨论(0)
  • 2020-12-04 16:46

    table view added without storyboard

    class ViewController: UIViewController  , UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
    
    
    @IBOutlet weak var textField: UITextField!
    
    var autocompleteTableView: UITableView!
    var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children","aaaaaaaaa","aaaaaaaaaaaaaaaaaaa","aaaaaaaaa","a","aa","aaa"]
    var autocompleteUrls = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        autocompleteTableView = UITableView(frame: CGRectMake(self.textField.bounds.minX,self.textField.bounds.maxY,self.textField.bounds.width,self.textField.bounds.height * 4), style: UITableViewStyle.Plain)
    
        textField.delegate = self
    
        autocompleteTableView.delegate = self
        autocompleteTableView.dataSource = self
        autocompleteTableView.scrollEnabled = true
        autocompleteTableView.hidden = false
    
        autocompleteTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(autocompleteTableView)
    }
    
    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
        var substring = (self.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
        {
            var myString:NSString! = curString as NSString
    
            var substringRange :NSRange! = myString.rangeOfString(substring)
    
            if (substringRange.location  == 0)
            {
                autocompleteUrls.append(curString)
            }
        }
    
        autocompleteTableView.reloadData()
        //autocompleteTableView.hidden = false
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return autocompleteUrls.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
                let autoCompleteRowIdentifier = "cell"
                var 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)!
        textField.text = self.autocompleteUrls[indexPath.row]
    
        self.autocompleteTableView.hidden = true
    }
    
    
    
    
    
    
    
    
    
    
    
     }
    
    0 讨论(0)
  • 2020-12-04 16:48

    Replace your searchAutocompleteEntriesWithSubstring function content with the one below. I hope it would help you.

    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 讨论(0)
提交回复
热议问题