2010-09-26 20 views

回答

14

如果 - 像我一樣 - 你認爲普通的TableView太難看了,你也可以放棄使用SearchDisplayController。

我剛:

  • 插入一個空查看一個搜索欄的TableView就像我們平時對IBOutlet中
  • 選擇文件的所有者委託他們兩個做。
  • 在開始部分的數量是0([myTab計數]),然後我使用reloadData,這次myTab由結果填充。
  • [self.resultTableView reloadData];

在這裏,你可以找到我從與會代表使用

@interface MyViewController : UIViewController <UIApplicationDelegate, UISearchBarDelegate> { 

    IBOutlet UISearchBar *searchBar; 
    IBOutlet UITableView *resultTableView; 
} 

@property (nonatomic, retain) IBOutlet UISearchBar *searchBar; 
@property (nonatomic, retain) IBOutlet UITableView *resultTableView; 

    //For your searchBar the 2 most important methods are 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBarClicked; 
- (BOOL)searchBarTextDidEndEditing; 


    //For your TableView the most important methods are in my case: 

    //number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
    //HEADERS 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 
    //different numbers of row by section 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
    //the cells themselves 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

@end 

畢竟所有的方法,最簡單的解決方案往往是最好的...

+1

這是一個很好的見解,並且真正簡化了搜索的使用酒吧在我的應用程序。謝謝! – Luke 2011-05-29 00:14:16

0

這是不可能的,因爲searchResultsTableView屬性是readonly

+0

所以基本上是絕對沒有辦法將其更改爲一個分組的風格?這是荒謬的,蘋果會阻止我們做這樣的事情.. – Noam 2010-09-26 05:27:29

+0

@David,我知道它很爛。 :( – 2010-09-26 05:36:13

1

你可以嘗試創建UISearchDisplayController的子類,並searchResultsTableView搜索

任何.h文件中加入:

@interface YourUISearchDisplayController : UISearchDisplayController { 
    UITableView * searchResultsTableView; 
} 

@property (nonatomic) UITableView * searchResultsTableView; 

@end; 

就用YourUISearchDisplayController代替OD UISearchDisplayController。注意:您可能必須使用(非原子,保留),(非原子,分配)或(非原子,複製)。我不太確定

+0

「然後,只需用YourUISearchDisplayController代替UISearchDisplayController」 所以這是我做過什麼: myUISearchDisplayController = [[MyUISearchDisplayController頁頭] initWithStyle:UITableViewStyleGrouped]; \t self.searchDisplayController.searchResultsTableView = myUISearchDisplayController.searchResultsTableView; 但它給我的錯誤,「/RootViewController.m:472:錯誤:對象無法設置 - 只讀屬性或沒有設置發現 」 有沒有解決方法? – Noam 2010-09-26 07:16:38

+0

@David,我不認爲這是可能的要覆蓋Objective-C中的屬性,只能覆蓋**方法**。 – 2010-09-26 09:32:30

+0

是啊,我看起來你運氣不好。即使你爲只讀屬性編寫setters,他們只是繼續調用自己。 – 2010-09-27 17:09:56

0

重寫-searchResultsTableView將無法​​正常工作,因爲UISearchDisplayController直接訪問它的表視圖實例變量,而不調用方法。

UISearchDisplayController的指定初始值設定項似乎是一個私有方法,-initWithSearchBar:contentsController:searchResultsTableViewStyle:,它設置_searchResultsTableViewStyle實例變量。此實例變量用於創建搜索結果表視圖。公共初始化程序調用此方法,傳遞UITableViewStylePlain

直接調用私有指定的初始化或設置實例變量可能會得到從App Store拒絕的應用程序,所以你可能而是試圖重寫公共初始化,並呼籲

[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped] 
     forKey:@"searchResultsTableViewStyle"]; 
3

這個工作對我來說:

創建延伸UISearchDisplayController類:

@interface RVSearchDisplayController : UISearchDisplayController 
@end 

@implementation RVSearchDisplayController 

-(UITableView *) searchResultsTableView { 
    [self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped] 
      forKey:@"_searchResultsTableViewStyle"]; 
    return [super searchResultsTableView]; 
} 

@end 

然後添加一個UISearchDisplayController添加到使用IB的表格中,並在Identity Inspector中將其定製類別更改爲RVSearchDisplayController

+3

這算作使用私人API嗎? – chakrit 2012-03-27 11:25:30

15

這個工作對我(的iOS 5.0):

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped] 
     forKey:@"_searchResultsTableViewStyle"]; 
+0

這實際上是這篇文章中的最佳解決方案。謝謝! – jcdmb 2012-05-08 07:41:12

+0

這也適用於我,iOS 5.1 – jxdwinter 2012-05-15 09:46:41

+6

'_searchResultsTableViewStyle'似乎沒有記錄......這是否會通過蘋果的批准? – 2012-09-11 07:28:58