【发布时间】:2016-10-30 05:17:22
【问题描述】:
这是我的UISearchDisplayController,如果我执行搜索并返回具有匹配起始字母的结果,它就会起作用。
但是,我要做的是启用对包含字符串的文本的搜索。
例如,如果我有数据chocolate chip 和dark 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