Category for UITextField to add auto suggestion feature. [Tutorial](http://shyngys.com/uitextfield_and_auto_suggestion).
[](https://travis-ci.org/Kassymov Shyngys/UITextField_AutoSuggestion)
To run the example project, clone the repo, and run pod install
from the Example directory first.
UITextField_AutoSuggestion is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "UITextField_AutoSuggestion"
You can use library this way:
Conform to UITextFieldAutoSuggestionDataSource
:
@interface ViewController () <UITextFieldAutoSuggestionDataSource>
Set data source to some UITextField
and start observing changes
// `fieldIdentifier` is optional
self.textField.autoSuggestionDataSource = self;
self.textField.fieldIdentifier = @"FIELD_ID";
[self.textField observeTextFieldChanges];
Implement required data source methods
#pragma mark - UITextFieldAutoSuggestionDataSource
- (UITableViewCell *)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
static NSString *cellIdentifier = @"AutoSuggestionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// configure cell
cell.textLabel.text = DATA[indexPath.row];
return cell;
}
- (NSInteger)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section forText:(NSString *)text {
return DATA.count;
}
Implement optional data source methods if needed
- (void)autoSuggestionField:(UITextField *)field textChanged:(NSString *)text {
// can be useful in some scenarious, see example project
[self loadDataFromInternet];
}
- (CGFloat)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
return 50;
}
- (void)autoSuggestionField:(UITextField *)field tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath forText:(NSString *)text {
// do something if suggestion row selected
NSLog(@"%@", DATA[indexPath.row]);
}
Kassymov Shyngys, [email protected]
UITextField_AutoSuggestion is available under the MIT license. See the LICENSE file for more info.