views:

244

answers:

3

In a table view I have set a UISearchBar, set the delegate, and add the protocol.

When user tap a word everything is okay except that the search of "tennis" is different from "Tennis".

How can I make the search bar a case-insensitive UISearchBar? Here is my code where I think evrything 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];
}
A: 

Theres some 'lowercaseString' function, don't know where I came across this seeing as I have nothing to do with iPhones, I'm sure if you google it you'll find something.

kalpaitch
A: 

When you do the comparison, try doing it with the lowercase version.

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

Just to clarify, this will not change any of the values, just return a lowercase version of the original, that you then use to do the comparison with,

RickiG
thanks guys really really helpful
Flodev03
+3  A: 

The easiest way to do this is probably

 NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
Noah Witherspoon
Thanks Noah! came across this on Google
ambertch