2014-09-10 49 views
1

我希望用戶能夠搜索莎士比亞獨白的表格視圖來找到他們最需要的。filterContentForSearchText同時爲一個對象的多個屬性

例如,在「萬事如意好」中有一個名爲海倫娜的角色,但在「仲夏夜之夢」中還有另一個角色名叫海倫娜。我希望用戶能夠在同一搜索欄中輸入角色的名稱和劇本的名稱以調出他們想要的結果

例如:搜索查詢「helena midsummer」只應該在「仲夏夜之夢」中提起海倫娜的獨白。

目前,在搜索「helena」: 時,結果顯示了「All's Well That End Well」和「仲夏夜之夢」的獨白。

但在搜索「海倫娜仲夏」或甚至只是「海倫娜」: 沒有結果。

我該如何允許filterContentForSearchText忽略所有空白區域,同時查看搜索查詢的任何方面是否匹配獨白的任何部分,無論是作者姓名,文本,標題,字符等。?

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
NSArray* words = [searchText componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]]; 
searchText = [words componentsJoinedByString:@""]; 

NSPredicate *p0 = [NSPredicate predicateWithFormat:@"title contains[cd] %@", searchText]; 
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"authorFirst contains[cd] %@", searchText]; 
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"authorLast contains[cd] %@", searchText]; 
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"text contains[cd] %@", searchText]; 
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"gender contains[cd] %@", searchText]; 
NSPredicate *p5 = [NSPredicate predicateWithFormat:@"tone contains[cd] %@", searchText]; 
NSPredicate *p6 = [NSPredicate predicateWithFormat:@"period contains[cd] %@", searchText]; 
NSPredicate *p7 = [NSPredicate predicateWithFormat:@"age contains[cd] %@", searchText]; 
NSPredicate *p8 = [NSPredicate predicateWithFormat:@"length contains[cd] %@", searchText]; 
NSPredicate *p9 = [NSPredicate predicateWithFormat:@"notes contains[cd] %@", searchText]; 
NSPredicate *p10 = [NSPredicate predicateWithFormat:@"tags contains[cd] %@", searchText]; 
NSPredicate *p11 = [NSPredicate predicateWithFormat:@"character contains[cd] %@", searchText]; 

NSPredicate *predicateAll = [NSCompoundPredicate orPredicateWithSubpredicates:@[p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11]]; 
searchResults = [monologuesArray filteredArrayUsingPredicate:predicateAll]; 
} 

回答

0

我在這裏能夠找到解決方案。

Core Data Query Multiple Columns with Single Search String

我的新filterContentForSearchText看起來是這樣的:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { 
NSString *sep = @", "; 
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep]; 
searchText = [searchText stringByReplacingOccurrencesOfString:@", " withString:@" "]; 
NSMutableArray *array = [searchText componentsSeparatedByCharactersInSet:set]; 

// The array splits up searchText based on dividing characters. 
// However, this split creates a new element immediately that just a "" string. This empties the search results. 
// This if statement ignores empty elements in the array, only recognizing a new element when a non-space character is entered. It's great! 
if ([array.lastObject isEqualToString:@""] || [array.lastObject isEqualToString:@" "]) { 
    // This keeps array from destroying itself and crashing. 
    if (array.count > 1) { 
     NSLog(@"last object was %@",array.lastObject); 
     [array removeLastObject]; 
    } 
} 

NSMutableArray *subPredicates = [NSMutableArray array]; 
for (NSString *q in array) { 
    [subPredicates addObject: 
    [NSPredicate predicateWithFormat:@"title contains[cd] %@ OR character contains[cd] %@ OR authorFirst contains[cd] %@ OR authorLast contains[cd] %@ OR text contains[cd] %@ OR gender contains[cd] %@ OR tone contains[cd] %@ OR period contains[cd] %@ OR age contains[cd] %@ OR length contains[cd] %@ OR notes contains[cd] %@ OR tags contains[cd] %@", q, q, q, q, q, q, q, q, q, q, q, q]]; 
} 

NSCompoundPredicate *predicate = [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType 
                   subpredicates:subPredicates]; 

searchResults = [monologuesArray filteredArrayUsingPredicate:predicate]; 
} 
相關問題