在表格视图中,我设置了一个UISearchBar,设置了委托,并添加了协议。
当用户点击一个单词时,除了搜索“网球”与“网球”不同外,一切都没问题。
如何使搜索栏成为不区分大小写的UISearchBar?这是我的代码,我认为事情发生了:
- (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];
}
答案 0 :(得分:14)
最简单的方法可能是
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
答案 1 :(得分:2)
试试这个。
- (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 :(得分:1)
进行比较时,请尝试使用小写版本。
NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];
只是为了澄清,这不会改变任何值,只返回原始的小写版本,然后用来与之进行比较,
答案 3 :(得分:0)
Theres有些'lowercaseString'功能,不知道我遇到了什么,因为我与iPhone无关,我敢肯定,如果你谷歌它你会找到一些东西。