启用UISearchDisplayController以搜索包含字符串的文本

时间:2016-10-30 05:17:22

标签: sql objective-c uisearchbar uisearchdisplaycontroller

这是我的UISearchDisplayController,如果我执行搜索并返回带有匹配的起始字母的结果,则有效。
但是,我要做的是启用包含字符串的文本搜索。
例如,如果我有数据chocolate chipdark chocolate,当我在搜索栏上输入chocolate时,我希望返回两个结果。

以下代码仅返回chocolate chip但不返回dark chocolate

self.searchDisplayController = [[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] autorelease];
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsTitle = NSLocalizedString(@"Matches", @"");

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSString *text = [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([text length]) {
        [self.db fetchCompletionListWithString:text callback:^(NSArray *list) {
            self.prefixArray = list;
            [self.searchDisplayController.searchResultsTableView reloadData];
        }];
    }
}

这是fetchCompletionListWithString

- (void)_fetchCompletionListWithString:(NSString *)inStr callback:(void(^)(NSArray *))inCallback
{
    if (!inStr || ![inStr length]) {
        return;
    }

    const char *SQL = "SELECT id,title FROM entries WHERE title LIKE ? || '%';";
    ObjSqliteStatement *statement = [[ObjSqliteStatement alloc] initWithSQL:SQL db:db];
    [statement bindText:inStr toColumn:1];
    NSMutableArray *list = [NSMutableArray array];
    while ([statement step]) {
        NSInteger entryID = [statement intFromColumn:0];
        NSString *title = [statement textFromColumn:1];
        if (entryID) {
            [list addObject:@{kMDIdentifierKey:@(entryID), kMDTitleKey:title}];
        }
    }
    [statement release];
    dispatch_async(dispatch_get_main_queue(), ^{
        inCallback(list);
    });
}

我尝试将*SQL更改为"SELECT id,title FROM entries WHERE title LIKE ? '%' || '%';"。但这不起作用。

任何建议将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

取决于您查询结果的位置。

如果查询数组,请使用NSPredicate过滤结果。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",searchToken];

如果您在Database上查询,请使用SQL查询:

SELECT * FROM mytable
WHERE name LIKE '%chocolate%'

希望得到这个帮助。

根据您的编辑,您可以尝试这种方式(因为您使用的库太旧了)。如果您想处理SQL,我建议您使用Realm

- (void)_fetchCompletionListWithString:(NSString *)inStr callback:(void(^)(NSArray *))inCallback
{
    if (!inStr || ![inStr length]) {
        return;
    }
inStr = [NSString stringWithFormat:@"%%%@%%",inStr];
    const char *SQL = "SELECT id,title FROM entries WHERE title LIKE ? ;";
    ObjSqliteStatement *statement = [[ObjSqliteStatement alloc] initWithSQL:SQL db:db];
    [statement bindText:inStr toColumn:1];
    NSMutableArray *list = [NSMutableArray array];
    while ([statement step]) {
        NSInteger entryID = [statement intFromColumn:0];
        NSString *title = [statement textFromColumn:1];
        if (entryID) {
            [list addObject:@{kMDIdentifierKey:@(entryID), kMDTitleKey:title}];
        }
    }
    [statement release];
    dispatch_async(dispatch_get_main_queue(), ^{
        inCallback(list);
    });
}