我也一直在爭奪與這類UISearchBarController
問題,最近幾天我自己,我不得不說做什麼異常與一個UISearchBar是最好的方式根本不使用UISearchDisplayController
!
只需使用UISearchBar
和UISearchBarDelegate
方法並自行打印,然後您就可以將所有內容設置爲完全按照您的要求操作。
這裏是我在最近的一個項目做。 - 範圍欄始終保持可見 - 我立即過濾作爲文本輸入 - 我立即過濾,如果範圍是改變 - 我隱藏取消按鈕時,不需要的話 - 我隱藏鍵盤的不需要的時候
// Filters the table when requested
- (void)filterContentForSearchBar:(UISearchBar *)searchBar
{
NSString *scope = [[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]];
NSString *search = [searchBar text];
NSMutableArray *predicates = [[NSMutableArray alloc] init];
if ([scope isEqualToString:@"Selected"])
{
[predicates addObject:[NSPredicate predicateWithFormat:@"selected == 1"]];
}
if (search && search.length) {
[predicates addObject:[NSPredicate predicateWithFormat:@"name contains[cd] %@", search]];
}
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
self.filteredObjectList = [self.objectList filteredArrayUsingPredicate:predicate];
[self.myTableView reloadData];
}
#pragma mark - UISearchBarDelegate Methods
// React to any delegate method we are interested in and change whatever needs changing
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = true;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = false;
[searchBar resignFirstResponder];
searchBar.text = nil;
[self filterContentForSearchBar:searchBar];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = false;
[searchBar resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self filterContentForSearchBar:searchBar];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[self filterContentForSearchBar:searchBar];
}
偉大工程:)
w ^雖然我可以在iOS6中使用此修復程序,但它在iOS7中無法使用。有沒有辦法讓它工作,同時還要維護搜索顯示控制器? – msec
此解決方案不適用於iOS 8.請建議。 – user1544494