Getting autocomplete to work in swift

前端 未结 12 531
清酒与你
清酒与你 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: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
    }
    
    
    
    
    
    
    
    
    
    
    
     }
    

提交回复
热议问题