問題的簡短說明: 我想設置搜索欄的文本,而不自動觸發與其綁定的搜索顯示控制器。searchdisplaycontroller:更改搜索欄的文本
問題的詳細描述: 我有一個帶有搜索欄和搜索顯示控制器的iphone應用程序。 searchdisplaycontroller用於自動完成。對於自動填充,我使用了一個sqlite數據庫。用戶輸入關鍵字的前幾個字母,結果顯示在searchdisplaycontroller的表格中。每個輸入的字符都會執行一個sql select查詢。這部分工作正常,字母已輸入,結果可見。
問題如下:如果用戶從表中選擇一行,我想將搜索欄的文本更改爲自動完成結果表中選定的文本。我也想隱藏搜索顯示控制器。這不起作用。搜索顯示控制器消失後,搜索欄中的文本框爲空。我不知道什麼是錯的。我並不認爲如此簡單的改變文本框的文本會變得如此複雜。
我試圖改變兩種方法中的文本: 首先在didSelectRowAtIndexPath方法中(對於搜索顯示控制器的搜索結果表),但這並沒有幫助。文本在那裏,而搜索顯示控制器是活動(動畫離開),但在此之後,文本框是空的。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRowAtIndexPath");
if (allKeywords.count > 0)
{
didSelectKeyword = TRUE;
self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row];
NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword);
shouldReloadResults = FALSE;
[[self.mainViewController keywordSearchBar] setText: selectedKeyword];
//shouldReloadResults = TRUE;
[[mainViewController searchDisplayController] setActive:NO animated: YES];
}
}
我也嘗試在searchDisplayControllerDidEndSearch方法中改變它,但那也沒有幫助。文本框是......再次......空白。
編輯:實際上它不是空的文本在那裏,但在消失的動畫後,自動完成表的結果仍然存在。所以它變得越來越糟糕。
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
NSLog(@"searchDisplayControllerDidEndSearch");
if (didSelectKeyword)
{
shouldReloadResults = FALSE;
NSLog(@"keyword sdc didendsearch: %@", selectedKeyword);
[[mainViewController keywordSearchBar] setText: selectedKeyword]; //
在這種方法中,我調用另一種方法,從sqlite中選擇數據。這部分工作。
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"shouldReloadResults : %i", shouldReloadResults);
if (shouldReloadResults)
{
NSLog(@"shouldReloadTableForSearchString: %@", searchString);
[self GetKeywords: searchString : @"GB"];
NSLog(@"shouldReloadTableForSearchString vege");
return YES;
}
return NO;
}
請問我是否不清楚我的問題是什麼。我需要你的幫助。謝謝
僅供參考:'searchDisplayController'在iOS 8中已棄用。請查看'UISearchController' – Besi 2015-06-12 13:17:51