Sajith K
Sajith K

Reputation: 11

Autocomplete textfield in ios

I am working in Xcode 6.4(Objective C). I have an array of names.I want to autocomplete my textfield while user enter something.If user enters "a" in TextField, a dropdown with all names which start from "a" must be shown.Like that if user enters "ab" in TextField, a dropdown with all names which start from "ab" must be shown.

I know a TableView is needed for dropdown.I tried some examples by searching SO and other sites(RayWendelich.com). But i couldn't solve this issue.Please help me with a simple solution.(I am new to the iOS development).

Upvotes: 0

Views: 502

Answers (2)

SKD
SKD

Reputation: 1477

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

     NSString *substring = [NSString stringWithString:textField.text];

     substring = [substring stringByReplacingCharactersInRange:range withString:string];

    [self searchFoodNameFromString:substring];

return YES;

}

-(void)searchFoodNameFromString:(NSString *)strFood {

if(strFood.length>2)
{
     [arrAutoSuggFood removeAllObjects];
    NSInteger counter = 0;
    for(NSString *name in itemArray)
    {
        NSRange r = [name rangeOfString:strFood options:NSCaseInsensitiveSearch];
        if(r.length>2)
        {
            [arrAutoSuggFood addObject:name];
        }

        counter++;

    }

    if (arrAutoSuggFood.count > 0)
    {
        NSLog(@"%@",arrAutoSuggFood);
        self.viewAutoSuggest.hidden = FALSE;
        [self displayAutoSuggestView];

    }
    else
    {
        self.viewAutoSuggest.hidden = TRUE;
    }

}
else
{
    [self.viewAutoSuggest setHidden:TRUE];

}

}

-(void)displayAutoSuggestView {

CGFloat autoSuggViewHeight;
if ([arrAutoSuggFood count] == 1) {
    autoSuggViewHeight = 44.0 * 1;
} else if ([arrAutoSuggFood count] == 2) {
    autoSuggViewHeight = 44.0 * 2;
} else {
    autoSuggViewHeight = 44.0 * 3;
}


[_viewAutoSuggest removeFromSuperview];
_viewAutoSuggest.frame = CGRectMake(addFoodAnotationTxt.frame.origin.x+8, addFoodAnotationTxt.frame.origin.y - autoSuggViewHeight, addFoodAnotationTxt.frame.size.width, autoSuggViewHeight);
[_scrlVwMain bringSubviewToFront:_viewAutoSuggest];
[_scrlVwMain addSubview:_viewAutoSuggest];

_tblAutoSugg.frame = self.viewAutoSuggest.bounds;
_tblAutoSugg.delegate = self;
_tblAutoSugg.dataSource = self;
[_viewAutoSuggest addSubview:_tblAutoSugg];
 [_tblAutoSugg reloadData];

}

Upvotes: 0

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

For that you can use shouldChangeCharactersInRange delegate method of UITextField.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *searchText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    for(NSString *strName in arrNames) {
        if([strName hasPrefix:searchText]) {
            // Entered text match with names array
            // Store it in temporary array.
        }
    }

   return YES;
}

Upvotes: 2

Related Questions