2011-06-22 58 views
1

我有一個數據結構(的plist),看起來是這樣的:搜索的NSArray(其中包含的NSDictionary的NSArray的,反覆)

enter image description here

我在這裏是NSDictionaryNSArray 。每個NSDictionary具有兩個密鑰:

Title 
Link (recursive) 

這形成樹狀結構,具有可變長度的分支即一些分支可以在0級死,有的可水平3或更大。

我在UITableView(在UINavigationController的幫助下)顯示了此結構。這很容易。

注意:在竊聽的葉節點 ,一個 事件被觸發,即模型窗口 出現一些信息(由NSDictionary對象 與作爲「鏈接」表示)。

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

搜索欄將出現在UITabeView的上方(對於0級)。我需要用一種方法來搜索這種樹狀結構,然後使用UISearchDisplayController來顯示結果,然後允許用戶瀏覽結果。

如何?...是我有點卡住 ,需要一些建議。

搜索必須快速,因爲我們想要搜索當您鍵入時搜索。

p.s.我曾想過將這種數據結構轉換爲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; 
} 

回答

1

核心數據能夠非常有效地進行數據存儲搜索,並會將搜索擴展到更高層次。此外,如果您使用NSFetchedResultsController作爲TableView,它幾乎肯定會提高內存效率 - 最糟糕的情況是在任何給定時間只能加載一個級別的數組。最好的情況要好得多,因爲它只有faulted幾個對象進入數組。 HTH