2012-02-07 47 views
2

我有一個UISearchBar和一個由xib設置的UISearchBarDisplayController。 搜索沒有結果後,它會在表格中顯示「無結果」。 如何修改默認文本?如何在UISearchDisplayController中將默認文本更改爲「無結果」

+0

7個問題,你已經標記爲無效接受?您之前的任何問題是否已經得到充分回答? – 2012-02-07 12:10:51

+0

我7個問題中的2個給了我幫助。我寫了反饋並標明答案已被接受。我不知道爲什麼它說0% – 2012-02-07 12:26:24

+0

我認爲這足以在反饋中標記爲已接受,但我現在知道如何標記左邊的標記以使其變爲綠色。 – 2012-02-07 12:37:08

回答

5
@interface ClassName : UIViewController { 
    UILabel      *noResultLabel; 
    UISearchDisplayController *searchController; 
} 

@implementation ClassName 

- (id) init { 
    // bla bla bla bla 
    // some more bla 
    // setup your UISearchDisplayController and stuffz 
    noResultLabel = nil; 
} 

- (void) changeNoResultsText:(NSString *)text { 
    if (noResultLabel == nil) { 
     for (id subview in searchController.searchResultsTableView.subviews) { 
      if ([subview isKindOfClass:[UILabel class]]) { 
       if ([((UILabel *)subview).text isEqualToString:@"No Results"]) { 
        if (noResultLabel == nil) noResultLabel = subview; 
       } 
      } 
     } 
    } 

    if (noResultLabel != nil) noResultLabel.text = text; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)bar { 
    [self changeNoResultsText:@"Searching..."]; 
} 

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self changeNoResultsText:@"Searching full search..."]; 
    return YES; 
} 

@end 
+4

不幸的是,如果語言環境是英語,這可能只會起作用。 – 2013-01-30 21:33:28

+0

是的,這隻適用於英語,您需要更改「無結果」來處理其他語言。 – EvilPenguin 2013-02-01 16:22:30

8

你也可以試試這個。

  1. 在搜索顯示控制器的表視圖代表/數據源,抑制-numberOfRowsInSection:以1
  2. 然後在您-cellForRowAtIndex最小:方法中,如果數據源計數爲零,更改的標籤表格視圖單元格類似於「正在搜索...」
相關問題