2009-07-31 52 views
42

通常,一個UISearchDisplayController被激活後,會調暗tableView並關注searchBar。只要在searchBar中輸入文本,它就會創建一個searchResultsTableView,它在searchBar和鍵盤之間顯示。當第二個UITableView被加載/顯示/隱藏/卸載時,searchDisplayController的代理被調用。通常它會在輸入時顯示實時搜索結果或自動完成條目。UISearchDisplayController沒有結果tableView?

在我的應用程序中,我想搜索一個web服務,並且我不想調用用戶輸入的每個字母的web服務。因此,我想完全禁用searchResultsTableView,並在輸入文本時保留變暗的黑色疊加層。一旦他點擊搜索按鈕,我會觸發搜索(帶有加載屏幕)。

只是返回零行searchResultsTableView看起來不太好,因爲它顯示一個空的searchResultsTableView與「沒有結果」消息。我試圖在出現的時候隱藏表格(searchDisplayController:didLoadSearchResultsTableView:),但黑色的暗淡疊加層也隱藏起來,這樣潛在的tableView就會再次完全可見。

除了重新創建UISearchDisplayController功能之外的任何想法?

回答

38

這裏是我剛剛想出 ,你也必須返回0的結果,而編輯搜索字符串

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    savedSearchTerm = searchString; 

    [controller.searchResultsTableView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]]; 
    [controller.searchResultsTableView setRowHeight:800]; 
    [controller.searchResultsTableView setScrollEnabled:NO]; 
    return NO; 
} 

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView 
{ 
    // undo the changes above to prevent artefacts reported below by mclin 
} 

我想你會想出做什麼小動作未來基於

+0

正是我在找的東西。非常感謝! – Zargony 2009-10-20 12:42:00

+1

第一次爲我工作,但如果你搜索,點擊x清除,然後輸入更多的文字,它顯示所有的行與不透明的黑色背景和白色的分界線。我猜它可能是在UITableCellViews,所以我說這個給你的功能和它的工作: 爲(UIView的*在self.searchDisplayController.searchResultsTableView.subviews子視圖){ \t \t [子視圖removeFromSuperview] } – mclin 2010-09-04 06:06:21

2

應該足以實現你UISearchDisplayDelegate下面的方法(這通常是自定義的UITableViewController子類)

- (BOOL) searchDisplayController: (UISearchDisplayController *) controller shouldReloadTableForSearchString: (NSString *) searchString 
{ 
    [self startMyCustomWebserviceSearchAsBackgroundProcessForString: searchString]; //starts new NSThread 
    return NO; 
} 

你試過嗎?

+0

這防止searchDisplayController阻止發送[searchResultsTableView重載],但不幸的是仍顯示searchResultsTableView(「無結果」消息) – Zargony 2009-08-01 14:13:50

+0

在此情況下恐怕,該UISearchDisplayController的默認行爲不能修改...對不起 – xoconoxtle 2009-08-01 16:28:57

+0

我昨天注意到,YouTube應用程序完全符合我的要求。不幸的是,我還沒找到任何好的方法(除了創建UISearchDisplayController)。 – Zargony 2009-08-12 20:43:14

19

你有沒有嘗試過這樣的:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_lookup:) object:nil]; 
[self performSelector:@selector(_lookup:) withObject:txt afterDelay:0.20]; 

這樣一來,如果用戶類型中的1/5秒另一個字符,你只會讓自己的網絡電話。

+1

與對象:無,取消電話會匹配嗎?如果你還沒有執行任何其他的執行請求,你可能最好不要調用普通的老的[NSObject cancelPreviousPerformRequestsWithTarget:self]。 – Thom 2012-08-27 17:33:23

2

下面的user182820的代​​碼是我的版本。我隱藏了UISearchDisplayController的表視圖。當在搜索框中輸入一個字符時,我會放置一個'變暗的視圖',使其看起來像UISearchDisplayController的'變暗視圖'永遠不會消失,然後在搜索結束時將其刪除。如果您輸入一些字符並按取消,表格視圖會短暫全白,而我不知道如何解決此問題。

- (void)viewDidLoad { 
    ... 
    tableViewMask=[UIView new]; 
    tableViewMask.backgroundColor = [UIColor blackColor]; 
    tableViewMask.alpha = 0.8; 
} 

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{ 
    tableViewMask.frame=CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y+controller.searchBar.frame.size.height, self.tableView.frame.size.width, self.tableView.frame.size.height-controller.searchBar.frame.size.height); 
    controller.searchResultsTableView.hidden=YES; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{ 
    [tableViewMask removeFromSuperview]; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ 
    if (searchString.length==0) 
     [tableViewMask removeFromSuperview]; 
    else 
     [self.tableView addSubview:tableViewMask]; 
    [searchText autorelease]; 
    searchText=[searchString retain]; 
    return NO; 
} 
1

我foud一個更好的辦法,因爲不存在與「最佳答案」的錯誤 - 分離器和「無結果」將滾動黑色背景表時顯示。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 

    controller.searchResultsTableView.backgroundColor = [UIColor blackColor]; 
    controller.searchResultsTableView.alpha = 0.8; 
    controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    for(UIView *subview in tableView.subviews) { 
     if([subview isKindOfClass:UILabel.class]) { 
      subview.hidden = YES; 
     } 
    } 

    return NO; 
} 

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { 

    self.searchDisplayController.searchResultsTableView.backgroundColor = [UIColor whiteColor]; 
    self.searchDisplayController.searchResultsTableView.alpha = 1; 
    self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 

    for(UIView *subview in tableView.subviews) { 
     if([subview isKindOfClass:UILabel.class]) { 
      subview.hidden = NO; 
     } 
    } 

    // search results and reload data .... 
} 
9

沒有上面似乎到底好工作,所以我有以下想出了(你必須調用removeTableHeader當您準備好顯示你的結果):

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 
    [self setTableHeader]; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    [self setTableHeader]; 
} 

- (void)setTableHeader { 
    UIView *headerView = [[UIView alloc] initWithFrame:self.searchDisplayController.searchResultsTableView.frame]; 
    headerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8]; 

    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor clearColor]]; 
    [self.searchDisplayController.searchResultsTableView setScrollEnabled:NO]; 
    [self.searchDisplayController.searchResultsTableView setTableHeaderView:headerView]; 

    [headerView release]; 
} 

- (void)removeTableHeader { 
    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor whiteColor]]; 
    [self.searchDisplayController.searchResultsTableView setScrollEnabled:YES]; 
    [self.searchDisplayController.searchResultsTableView setTableHeaderView:nil]; 
} 

顯然,它使表格透明,添加與表格大小相同的黑色/半透明表格標題,並禁用表格上的滾動,以便不會超出標題或超過標題。作爲獎勵,你可以添加一些東西到標題視圖('請稍等...'或一個活動指標)。

0

我想我找到了一個更好的實現這個問題。之前的所有答案都正確地顯示了與搜索前UITableView相似的灰色視圖,但是每個解決方案都缺少點擊該區域以取消搜索的功能。

因此,我認爲這個代碼效果更好。

首先,創建一個BOOL,如searchButtonTapped來指示是否點擊搜索按鈕。默認情況下它是NO。

然後:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
if (!searchButtonTapped) {   
    // To prevent results from being shown and to show an identical look to how the tableview looks before a search 
    [controller.searchResultsTableView setBackgroundColor:[UIColor clearColor]]; 
    [controller.searchResultsTableView setRowHeight:160]; 
    self.searchDisplayController.searchResultsTableView.scrollEnabled = NO; 
} else { 
    // Restore original settings 
    [controller.searchResultsTableView setBackgroundColor:[UIColor whiteColor]]; 
    [controller.searchResultsTableView setRowHeight:44]; 
    self.searchDisplayController.searchResultsTableView.scrollEnabled = YES; 
} 

return YES; 
} 

這個現在應該基於他的答案是明確的。請確保在用戶點擊搜索按鈕時恢復原始設置。

此外,在cellForIndexPath方法增加:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.contentView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8]; 

爲了創建輸入文本之前所顯示的相同的變暗視圖。確保將這些屬性應用於正確的單元格,即檢查哪個UITableView處於活動狀態,並且用戶沒有點擊搜索按鈕。

然後,重要的是,在didSelectRowAtIndexPath方法:

if (tableView == self.searchDisplayController.searchResultsTableView) { 
    if (searchButtonTapped) { 
      // Code for when the user select a row after actually having performed a search 
    { 
else 
    [self.searchDisplayController setActive:NO animated:YES]; 

現在,用戶可以點擊暗淡的區域,這會不會導致一個UITableViewCell的可見選擇,而是取消搜索。

9

有同樣的問題,因爲你,我處理得由一)開始搜索時,再由B中的searchResultsTableView的alpha設置爲0)添加/刪除的OverlayView的到ViewController的看法。對我來說就像一個魅力。

@interface MyViewController() 
//... 
@property(nonatomic, retain) UIView *overlayView; 
//... 
@end 

@implementation MyViewController 
@synthesize overlayView = _overlayView; 

//... 

- (void)viewDidLoad 
{ 
    //... 

    //define your overlayView 
    _overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 480)]; 
    _overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]; 
} 

//hide the searchResultsTableView 
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller 
{ 
    self.searchDisplayController.searchResultsTableView.alpha = 0.0f; 
} 

//when ending the search, hide the overlayView 
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller 
{ 
    [_overlayView removeFromSuperview]; 
} 

//depending on what the user has inputed, add or remove the overlayView to the view of the current viewController 
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 

    if ([searchString length]>0) 
    { 
     [self.view addSubview:_overlayView]; 
    } 
    else 
    { 
     [_overlayView removeFromSuperview]; 
    } 

    return NO; 
} 

@end 
2

什麼只是做這樣簡單:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
self.searchDisplayController.searchResultsTableView.hidden=YES; 
return YES; 
} 

工作正常..

1

現有的所有答案都過於複雜。您只需立即隱藏結果表視圖即可離開。

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView 
{ 
    tableView.hidden = YES; 
} 
相關問題