2015-01-26 66 views
0

我有一個主plist文件,其中包含一個集合視圖數據源。該文件具有根數組元素,其中包含以下格式的每個類別的詞典:
Example Layout搜索欄 - 爲收藏查看數據源過濾搜索項數組

該示例顯示了一個「類別」。當我建立細胞創建爲indexPath.row一個字典實例,並訪問該相關對象這些被加載到用於UICollectionView陣列和屬性訪問

self.mainDataSource = [[NSArray alloc] initWithContentsOfFile:plistPath]; 

。所有這些工作正常。

問題
我現在要做的是添加一個搜索欄。我有所有的設置,但我想根據輸入的字詞與每個類別可能具有的'sub'搜索字詞數組進行比較來篩選類別。

我有以下搜索時運行的方法,但我無法找出搜索類別字典'sub'數組的最佳方法。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self contains[c] %@", searchText]; 
    self.dataSourceForSearchResult = [self.mainDataSource filteredArrayUsingPredicate:resultPredicate]; 
} 

回答

1
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"(ANY SearchTerms LIKE[cd] %@ OR ANY SearchTerms CONTAINS[cd] %@)", searchText, searchText]; 
self.dataSourceForSearchResult = [self.mainDataSource filteredArrayUsingPredicate:resultPredicate]; 

訣竅是包括「任意」專爲喜歡的工作。然後將相關子數組的名稱作爲關鍵字。