filterContentForSearchText同时用于对象的多个属性

时间:2014-09-10 20:24:10

标签: ios objective-c xcode uitableview uisearchbar

我希望用户能够搜索莎士比亚独白的表格视图,找到他们最需要的那个。

例如,有一个名为Helena的角色,并且#34;所有的结果都很好......#34;但是还有另一个角色Helena in" A Midsummer Night&#39 ; s Dream"。我希望用户能够在同一个搜索栏中输入角色名称和游戏名称,以显示他们想要的结果

例如:搜索查询" helena仲夏"应该只出现海伦娜在“仲夏夜之梦”中所说的独白#34;

目前,在搜索" helena": 结果显示来自两个人的独白......所有人的结局都很好"和#34;仲夏夜之梦"。

但在搜索" helena仲夏"甚至只是" helena": 没有结果。

我如何允许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];
}

1 个答案:

答案 0 :(得分: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];
}