create an auto complete textfield in swift

To create an auto-complete textfield in Swift, you can follow these steps:

  1. Add a UITextField to your ViewController in the Storyboard.

  2. Create an array of strings that will be the basis for your auto-completion suggestions. For example:

main.swift
let suggestions = ["Apple", "Banana", "Cherry", "Dragonfruit", "Elderberry", "Fig", "Grape", "Honeydew", "Kiwi", "Lemon"]
122 chars
2 lines

Add a variable to keep track of the current suggestions:

main.swift
var currentSuggestions: [String] = []
38 chars
2 lines
  1. Implement the UITableViewDataSource and UITableViewDelegate protocols.
main.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    //...
}
97 chars
4 lines
  1. Set the dataSource and delegate properties of the UITableView to the ViewController.
main.swift
override func viewDidLoad() {
    super.viewDidLoad()
    //...
    self.tableView.dataSource = self
    self.tableView.delegate = self
}
138 chars
7 lines
  1. Implement the tableView(_:numberOfRowsInSection:) method to return the number of suggestions.
main.swift
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.currentSuggestions.count 
}
130 chars
4 lines
  1. Implement the tableView(_:cellForRowAt:) method to display the suggestion.
main.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    cell.textLabel?.text = self.currentSuggestions[indexPath.row]
    return cell
}
251 chars
6 lines
  1. Implement the textField(_:shouldChangeCharactersIn:replacementString:) method to filter the suggestions as the user types.
main.swift
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let searchText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    self.filterSuggestions(searchText)
    self.tableView.reloadData()
    return true
}
311 chars
7 lines
  1. Implement the filterSuggestions(_:) method to filter the suggestions and update the currentSuggestions variable.
main.swift
func filterSuggestions(_ searchText: String) {
    if searchText.isEmpty {
        self.currentSuggestions = []
    } else {
        self.currentSuggestions = self.suggestions.filter { $0.lowercased().contains(searchText.lowercased()) }
    }
}
245 chars
8 lines
  1. Show the table view when the user taps on the text field.
main.swift
func textFieldDidBeginEditing(_ textField: UITextField) {
    self.tableView.isHidden = false
}
96 chars
4 lines
  1. Hide the table view when the user stops editing the text field.
main.swift
func textFieldDidEndEditing(_ textField: UITextField) {
    self.tableView.isHidden = true
}
93 chars
4 lines
  1. Select the suggestion when the user taps on a suggestion in the table view.
main.swift
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.textField.text = self.currentSuggestions[indexPath.row]
}
147 chars
4 lines

And that's it! You should have a working auto-complete textfield in Swift.

gistlibby LogSnag