0

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;

}
Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
siddle
  • 137
  • 2
  • 10
  • 1
    Possible duplicate of [NSPredicate - case insensitive filtering for multiple conditions](https://stackoverflow.com/questions/26837990/nspredicate-case-insensitive-filtering-for-multiple-conditions) – Larme Nov 13 '17 at 12:25

2 Answers2

0

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.

Priya
  • 735
  • 5
  • 10
0

Use UITextfieldDeleage,UITableViewDelegate,UITableViewDataSource

@interface ABCViewController : UIViewController <UITextFieldDelegate> 
myTextField.delegate = self;

1.Get the TextField Text and Save the Value in NSMutableArray

2.Retrieve the Array value and check if the TextField Text is already contains in Array using NSPredicate

3.Show the TableViewCell TextLabel Text to Textfield

 #pragma mark - UITextField Delegate
 -(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];
 autoCompleteFilterArray = [autoCompleteArray filteredArrayUsingPredicate:predicate];
 NSLog(@"%@", autoCompleteFilterArray);

 if ([autoCompleteFilterArray count]==0) {
    autoCompleteTableView.hidden = TRUE;
 }else{
    autoCompleteTableView.hidden = FALSE;
 }
 [autoCompleteTableView reloadData];

return TRUE;
}
Mukesh
  • 777
  • 7
  • 20