ios - 搜索 NSDictionary 的 NSArray(其中包含 NSDictionary 的 NSArray,重复)

标签 ios search tree nsarray nsdictionary

我有一个数据结构(在 plist 中),看起来像这样:

enter image description here

我这里有一个NSDictionaryNSArray。每个 NSDictionary 都有两个键:

Title
Link (recursive)

这形成了一个树状结构,具有可变长度的分支,即一些分支可以在第 0 级死亡,而一些可以与第 3 级或更多一样大。

我在 UITableView 中展示了这个结构(在 UINavigationController 的帮助下)。这很容易。

Note: On tapping the Leaf Node (represented by NSDictionary object with Nil or Zero as "Link"), an event is triggered i.e. Model window appears with some information.

现在,我需要添加搜索支持。

搜索栏将出现在 UITabeView 上方(对于级别 0)。我需要想出一种方法来搜索这种树状结构,然后使用 UISearchDisplayController 显示结果,然后还允许用户导航结果。

How?... is where i'm a little stuck and need some advise.

搜索必须快速,因为我们希望边输入边搜索

附注我曾经想过把这个数据结构翻译成CoreData,现在还一直在脑海里徘徊。如果您认为这对这种情况有帮助,请提出建议。


编辑: 这是我当前的解决方案,它正在运行(顺便说一句):

#pragma mark -
#pragma mark UISearchDisplayController methods 

- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"%s", __FUNCTION__);
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    NSLog(@"%s", __FUNCTION__);
    [self filterCategoriesForSearchText:searchString
                               scope:[controller.searchBar selectedScopeButtonIndex]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    NSLog(@"%s", __FUNCTION__);
    [self filterCategoriesForSearchText:[controller.searchBar text]
                               scope:[controller.searchBar selectedScopeButtonIndex]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

#pragma mark UISearchDisplayController helper methods

- (void)filterCategoriesForSearchText:(NSString *)searchText scope:(NSInteger)scope {
    self.filteredCategories = [self filterCategoriesInArray:_categories forSearchText:searchText];

    NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:KEY_DICTIONARY_TITLE ascending:YES] autorelease];
    [self.filteredCategories sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
}

- (NSMutableArray *)filterCategoriesInArray:(NSArray *)array forSearchText:(NSString *)searchText {
    NSMutableArray *resultArray = [NSMutableArray array];
    NSArray *filteredResults = nil;

    // Apply filter to array
    // For some weird reason this is not working. Any guesses? [NSPredicate predicateWithFormat:@"%@ CONTAINS[cd] %@", KEY_DICTIONARY_TITLE, searchText];
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"Title CONTAINS[cd] %@", searchText];
    filteredResults = [array filteredArrayUsingPredicate:filter];

    // Store the filtered results (1)
    if ((filteredResults != nil) && ([filteredResults count] > 0)) {
        [resultArray addObjectsFromArray:filteredResults];
    }

    // Loop on related records to find the matching results
    for (NSDictionary *dictionayObject in array) {
        NSArray *innerCategories = [dictionayObject objectForKey:KEY_DICTIONARY_LINK];

        if ((innerCategories != nil) && ([innerCategories count] > 0)) {
            filteredResults = [self filterCategoriesInArray:innerCategories forSearchText:searchText];

            // Store the filtered results (2)
            if ((filteredResults != nil) && ([filteredResults count] > 0)) {
                [resultArray addObjectsFromArray:filteredResults];
            }
        }
    }

    return resultArray;
}

最佳答案

Core Data 能够非常有效地在数据存储中执行搜索,并将搜索有效地扩展到更多级别。此外,如果您使用 NSFetchedResultsController对于 TableView,它几乎肯定会提高内存效率——最坏的情况是在任何给定时间只加载一级数组。最好的情况要好得多,因为它只有 faulted。数组中的几个对象。

关于ios - 搜索 NSDictionary 的 NSArray(其中包含 NSDictionary 的 NSArray,重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6436263/

相关文章:

ios - NSNotification 不工作

android - 一个 AndroidManifest.xml 中的两个 searchable.xml Activity

algorithm - 开发用于高效搜索的数据库模式

regex - 如何在 linux shell 脚本中使用正则表达式搜索文件

Java执行字符串startsWith的最佳方法

algorithm - 在树数据结构中查找所有叶节点的最高效方法

ios - UIActivityViewController 排除所有 UIActivityCategoryAction

ios - 当内存消耗攀升但 Leaks 未检测到泄漏时,如何处理 iOS 中的泄漏?

ios - 如何通过我的表格 View 单元格的更新 View 函数传递一个字符串数组?

c# - 有关如何处理关系模型中的层次树的任何提示?