Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

ios - AutoComplete txtfield from array items in Objective c

I have a textfield in which whenever user type it shows related items from array in tableView to select any of them but when user type anything in small keyword it does not show the array. when user enter any word in capital letters as store in an array it shows the array.

i want that when user enter any word whether in small or capital letter it should show the table view containing array. Following is my code,

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

    NSLog(@"Range:%@",NSStringFromRange(range));
    NSLog(@"%@",textField.text);

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

    NSLog(@"%@",passcode);


    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF CONTAINS %@",passcode];

    carArray = [_staticCarArray filteredArrayUsingPredicate:predicate];

    city = [_staticCarArrays filteredArrayUsingPredicate:predicate];


    NSLog(@"%@", carArray);
    NSLog(@"%@", city);


    if ([carArray count]==0) {
        _carTable.hidden = TRUE;
    }else{
        _carTable.hidden = FALSE;
    }

    if ([city count]==0) {
        _autotable.hidden = TRUE;
    }else{
        _autotable.hidden = FALSE;
    }

    [_carTable reloadData];
    [_autotable reloadData];


    return TRUE;

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For case insenstive search Replace

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF CONTAINS %@",passcode];

with

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF CONTAINS[c] %@",passcode];

Here, [c] indicates case insensitive comparison.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...