【问题标题】:Enable UISearchDisplayController to search text contains string启用 UISearchDisplayController 以搜索包含字符串的文本
【发布时间】:2016-10-30 05:17:22
【问题描述】:

这是我的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 ? '%' || '%';"。但这不起作用。

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

【问题讨论】:

    标签: sql objective-c uisearchbar uisearchdisplaycontroller


    【解决方案1】:

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

    如果你查询一个数组,使用 NSPredicate 来过滤这样的结果。

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

    如果您在数据库上查询,请使用这样的 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);
        });
    }
    

    【讨论】:

    • 谢谢,但我收到 use of undeclared identifier 'str'unexpected interface name 'NSString': expected expression 错误。
    猜你喜欢
    • 2021-06-12
    • 2019-05-17
    • 2013-06-26
    • 2013-11-10
    • 2012-10-07
    • 2011-05-25
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多