UITableView / UISearchBar 返回不正确的结果

发布于 2024-10-20 14:27:59 字数 830 浏览 2 评论 0原文

我正在尝试在 UITableView 中实现搜索。搜索时,似乎返回了正确数量的结果,但我收到的条目来自结果中的原始故事数组,而不是 searchResults。我可以看到 searchResults 数组应该是数据源,但经过大量搜索后一直无法弄清楚如何使用 NSDictionaries 数组来实现它。任何帮助表示赞赏。

- (void)handleSearchForTerm:(NSString *)searchTerm {
[self setSavedSearchTerm:searchTerm];

if ([self searchResults] == nil)
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setSearchResults:array];
    [array release], array = nil;
}

[[self searchResults] removeAllObjects];    

if ([[self savedSearchTerm] length] != 0)
{

    for (NSDictionary *currentItem in [self stories])
    {
        if ([[currentItem objectForKey:@"title"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            [[self searchResults] addObject:currentItem];
        }
    }
}

}

I am attempting to implement searching in a UITableView. When searching, it appears that the correct number of results are returned, but I am receiving entries from the original stories array in the results, rather than searchResults. I can see that the searchResults array should be the data source, but haven't been able to figure out after tons of searching quite how to pull it off with an array of NSDictionaries. Any help is appreciated.

- (void)handleSearchForTerm:(NSString *)searchTerm {
[self setSavedSearchTerm:searchTerm];

if ([self searchResults] == nil)
{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self setSearchResults:array];
    [array release], array = nil;
}

[[self searchResults] removeAllObjects];    

if ([[self savedSearchTerm] length] != 0)
{

    for (NSDictionary *currentItem in [self stories])
    {
        if ([[currentItem objectForKey:@"title"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            [[self searchResults] addObject:currentItem];
        }
    }
}

}

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

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

发布评论

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

评论(2

夏日浅笑〃 2024-10-27 14:27:59

[tableView isEqual:self.searchDisplayController.searchResultsTableView] 也是创建和管理您自己的 BOOL isFiltering; 变量的另一种替代方法

[tableView isEqual:self.searchDisplayController.searchResultsTableView] is also another alternative to making and managing your own BOOL isFiltering; variable

靑春怀旧 2024-10-27 14:27:59

使用 NSPredicate 进行过滤

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"self.title MATCHES %@",searchTerm];

假设您的原始数组是“originalArray”,因此要获取过滤后的数组,请使用它再创建两个全局变量

 NSArray* filteredArray;
 BOOL isFiltering;

现在在搜索栏委托方法中执行以下操作

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
      NSPredicate* predicate = [NSPredicate predicateWithFormat:@"self.title MATCHES %@",searchTerm];
      filteredArray = [[originalArray filteredArrayUsingPredicate:predicate] retain];
}

现在您需要更改我将位您的表视图委托和数据源, .... 对于您正在使用的所有地方,

NSDictionary *currentString = [originalArray objectAtIndex:indexPath.row];

请使用以下内容

NSDictionary *currentString;
if(isFiltering)
      currentString = [originalArray objectAtIndex:indexPath.row];
else
      currentString = [filteredArray objectAtIndex:indexPath.row];

use NSPredicate for filtering

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"self.title MATCHES %@",searchTerm];

Suppose that your original array is "originalArray" so to get the filtered array use this make two more global variables

 NSArray* filteredArray;
 BOOL isFiltering;

Now in search bar delegate method do following

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
      NSPredicate* predicate = [NSPredicate predicateWithFormat:@"self.title MATCHES %@",searchTerm];
      filteredArray = [[originalArray filteredArrayUsingPredicate:predicate] retain];
}

Now you need to change l'll bit your table view delegate and data source, .... for all the places where you are using

NSDictionary *currentString = [originalArray objectAtIndex:indexPath.row];

use following

NSDictionary *currentString;
if(isFiltering)
      currentString = [originalArray objectAtIndex:indexPath.row];
else
      currentString = [filteredArray objectAtIndex:indexPath.row];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文