Is the UISearch case not appropriate?

In the table view, I installed UISearchBar, installed the delegate, and added the protocol.

When a user clicks on a word, everything is in order, except that the search for “tennis” is different from “Tennis”.

How can I make the search bar case-sensitive UISearchBar? Here is my code where I think everything happens:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
 [tableData removeAllObjects];// remove all data that belongs to previous search
 if([searchText isEqualToString:@""]||searchText==nil){
  [myTableView reloadData];
  return;
 }
 NSInteger counter = 0;
 for(NSString *name in dataSource)
 {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
  NSRange r = [name rangeOfString:searchText];
  if(r.location != NSNotFound)
   [tableData addObject:name];
  counter++;
  [pool release];
 }
 [myTableView reloadData];
}
+5
source share
4 answers

The easiest way to do this is probably

 NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
+13
source

Try it, it really helps you. If yes, then do not forget to vote for me

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

    [DetailSearchArray removeAllObjects];// remove all data that belongs to previous search

    if([searchText isEqualToString:@""]){

        [table reloadData];

        return;

    }

    NSInteger counter = 0;
    NSArray *result = [[NSArray alloc] initWithArray:[resultArray filteredArrayUsingPredicate:predicate]];

    for (NSString *name in  resultArray  ) {


        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
        //NSRange r=[name rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
        if(r.location != NSNotFound)

        {
            if(r.location== 0)//that is we are checking only the start of the names.



            {
                [DetailSearchArray addObject:name]; 

            }

        }

        }
        counter++;
        [pool release];

    }

    [table reloadData];

}
+2
source

, .

NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];

, , , ,

+1
source

Theres some "lowercaseString" function, I don’t know where I came across this observation, since I have nothing to do with the iPhone, I’m sure that if you do, you will find something.

0
source

All Articles