UISearch栏不区分大小写?

发布于 2024-08-25 13:51:42 字数 719 浏览 6 评论 0原文

在表视图中,我设置了 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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花之痕靓丽 2024-09-01 13:51:43

最简单的方法可能是

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

The easiest way to do this is probably

 NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
自此以后,行同陌路 2024-09-01 13:51:43

试试这个。

- (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];
}

Try this.

- (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];
}
染火枫林 2024-09-01 13:51:43

当您进行比较时,请尝试使用小写版本进行比较。

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

只是为了澄清,这不会更改任何值,只是返回原始值的小写版本,然后用它来进行比较,

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,

简美 2024-09-01 13:51:43

有一些“lowercaseString”函数,不知道我在哪里遇到这个,因为我与iPhone无关,我相信如果你用谷歌搜索你会找到一些东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文