이것은 검색을 수행하고 시작 알파벳과 일치하는 결과를 반환 할 경우 작동하는 내 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];
}];
}
}
이 내가 *SQL
"SELECT id,title FROM entries WHERE title LIKE ? '%' || '%';"
에 변경하려고 한 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);
});
}
입니다. 그러나 이것은 작동하지 않습니다.
모든 의견을 환영합니다. 미리 감사드립니다.
감사를 사용하는 것이 좋습니다,하지만 난'선언되지 않은 식별자 'str''와'예기치 않은 인터페이스 이름'는 NSString '의 사용을 얻을 : 예상 expression' 오류를. – user2872856
그것은 작동합니다. 고맙습니다! – user2872856