To create an auto-complete textfield in Swift, you can follow these steps:
Add a UITextField
to your ViewController
in the Storyboard.
Create an array of strings that will be the basis for your auto-completion suggestions. For example:
main.swift122 chars2 lines
Add a variable to keep track of the current suggestions:
main.swift38 chars2 lines
main.swift97 chars4 lines
dataSource
and delegate
properties of the UITableView
to the ViewController.main.swift138 chars7 lines
tableView(_:numberOfRowsInSection:)
method to return the number of suggestions.main.swift130 chars4 lines
tableView(_:cellForRowAt:)
method to display the suggestion.main.swift251 chars6 lines
textField(_:shouldChangeCharactersIn:replacementString:)
method to filter the suggestions as the user types.main.swift311 chars7 lines
filterSuggestions(_:)
method to filter the suggestions and update the currentSuggestions
variable.main.swift245 chars8 lines
main.swift96 chars4 lines
main.swift93 chars4 lines
main.swift147 chars4 lines
And that's it! You should have a working auto-complete textfield in Swift.
gistlibby LogSnag