0
这是我的UISearchDisplayController
,如果我执行搜索并返回匹配的起始字母表的结果。
但是,我要做的是启用搜索包含字符串的文本。
例如,如果我有数据chocolate chip
和dark chocolate
,当我在搜索栏上输入chocolate
时,我希望返回两个结果。
下面的代码只返回chocolate chip
但不dark chocolate
:启用UISearchDisplayController搜索文本包含字符串
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 ? '%' || '%';"
。但这不起作用。
任何建议将不胜感激。 在此先感谢。
谢谢,但我得到'使用未声明的标识符“str''和'意外接口名称‘的NSString’的:预期expression'错误。 – user2872856
这是工作。谢谢! – user2872856