Implementing Autocomplete in iOS
You may want to use this repo HTAutocompleteTextField, perfect solution.
How to implement Autocomplete in UISearchbar without UITableview?
First, you need to have a NSArray containing all of the data that you want to provide as potential options for the user. here i am using an NSMutableArray of pastURLs, and every time the user browses to a URL we’ll add it to the array.
Next, you need to create a view to display the URLs that the user can select from. One good way of doing this is just to create a table view below the input field that lists all of the potential options. This table view can appear only when the user is typing data into the text field, and can be hidden the rest of the time.
autocompleteTableView = [[UITableView alloc] initWithFrame:
CGRectMake(0, 80, 320, 120) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];
Use UITextFieldDelegate
and implementing the shouldChangeCharactersInRange
protocol.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring
stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
// Put anything that starts with this substring into the autocompleteUrls array
// The items in this array is what will show up in the table view
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
for more details check : Tutorial
Or download demo app: Demo App
how to implement autocomplete in google search for a IOS application?
You can retrieve the google suggestions in json format by sending a request to http://google.com/complete/search?output=toolbar&client=chrome&q=SEARCHTERM
Implement a autocomplete textfield Objective C
You can build this behaviour yourself using the UITextFieldDelegate
methods
( implement the delegate in your UIView
@interface someViewController : UIViewController <UITextFieldDelegate>
In doing this you get access to whatever the user has typed in
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Where you can compare it to the items in your array and display another UIView
or a custom button that when selected populates your text field.
Don't forget to tell your textfield
who it's delegate should be, probably in your viewDidLoad
method, but can also be done in the xib view
myTextField.delegate = self;
I know this seems laborious but it will be extremely gratifying.
Here's the apple doc for the UITextViewDelegate
Trick to get appropriate auto complete method in xcode
You just start typing cellForRowAtIndexPath
you will get that particular method. No need to type from tableView
...blah blah... Just type the methods main phrase, then you will get correct method in suggestion.
Thanks:)
How do I implement autocomplete table view google maps swift?
You are printing an attributed string. This should not appear when you display it in a UILable
'sattributedText
property. See my example below
when you call for result in results
save results in an array and reload the tableview by calling tableView.reloadData
. In your tableView data source set the number of rows to the number of items in your array. Then inside cellForRowAtIndexPath
do the following
let result = resultArray[indexPath.row] as? GMSAutocompletePrediction
cell.textLabel.attributedText = result.attributedPrimaryText
How to implement auto-complete for address using Apple Map Kit
Update - I've created a simple example project here using Swift 3 as the original answer was written in Swift 2.
In iOS 9.3 a new class called MKLocalSearchCompleter
was introduced, this allows the creation of an autocomplete solution, you simply pass in the queryFragment as below:
var searchCompleter = MKLocalSearchCompleter()
searchCompleter.delegate = self
var searchResults = [MKLocalSearchCompletion]()
searchCompleter.queryFragment = searchField.text!
Then handle the results of the query using the MKLocalSearchCompleterDelegate
:
extension SearchViewController: MKLocalSearchCompleterDelegate {
func completerDidUpdateResults(completer: MKLocalSearchCompleter) {
searchResults = completer.results
searchResultsTableView.reloadData()
}
func completer(completer: MKLocalSearchCompleter, didFailWithError error: NSError) {
// handle error
}
}
And display the address results in an appropriate format:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let searchResult = searchResults[indexPath.row]
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = searchResult.title
cell.detailTextLabel?.text = searchResult.subtitle
return cell
}
You can then use a MKLocalCompletion
object to instantiate a MKLocalSearch.Request
, thus gaining access to the MKPlacemark
and all other useful data:
let searchRequest = MKLocalSearch.Request(completion: completion!)
let search = MKLocalSearch(request: searchRequest)
search.startWithCompletionHandler { (response, error) in
if error == nil {
let coordinate = response?.mapItems[0].placemark.coordinate
}
}
Related Topics
iOS Document/File Type Associations - Wildcard Uti
How to Get Current Location Using Cllocationmanager in iOS
Phimageresultisdegradedkey/Phimagefileurlkey Is Not Found
Best Method to Store Data for an iOS App
iOS Web App: Showing Content Only If the Application Is Standalone
Reordering Uitableview Without Reorder Control
How to Detect Whether an iOS Device Is in Silent Mode or Not
How to Emulate Nfc Cards in iOS 13
Two Buttons Inside Hstack Taking Action of Each Other
Physicsbody Doesn't Adhere to Node's Anchor Point
Declaring Global Variables in Swift
Add a Navigation Bar to a View Without a Navigation Controller
App Crashes in Background While Changing Permission - Swift
How to Code the Launchscreen Programmatically