我有一個searchDisplayController
搜索UITableView
。將searchDisplayController表視圖樣式更改爲分組?
輸入搜索條件後,我可以看到另一個包含搜索結果的UITableView
。不過,我希望這UITableView
是GROUPED,而不是PLAIN(就像默認情況下那樣)。
我該怎麼做?
我有一個searchDisplayController
搜索UITableView
。將searchDisplayController表視圖樣式更改爲分組?
輸入搜索條件後,我可以看到另一個包含搜索結果的UITableView
。不過,我希望這UITableView
是GROUPED,而不是PLAIN(就像默認情況下那樣)。
我該怎麼做?
如果 - 像我一樣 - 你認爲普通的TableView太難看了,你也可以放棄使用SearchDisplayController。
我剛:
[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
畢竟所有的方法,最簡單的解決方案往往是最好的...
這是不可能的,因爲searchResultsTableView
屬性是readonly
。
所以基本上是絕對沒有辦法將其更改爲一個分組的風格?這是荒謬的,蘋果會阻止我們做這樣的事情.. – Noam 2010-09-26 05:27:29
@David,我知道它很爛。 :( – 2010-09-26 05:36:13
你可以嘗試創建UISearchDisplayController的子類,並searchResultsTableView搜索
任何.h文件中加入:
@interface YourUISearchDisplayController : UISearchDisplayController {
UITableView * searchResultsTableView;
}
@property (nonatomic) UITableView * searchResultsTableView;
@end;
就用YourUISearchDisplayController代替OD UISearchDisplayController。注意:您可能必須使用(非原子,保留),(非原子,分配)或(非原子,複製)。我不太確定
「然後,只需用YourUISearchDisplayController代替UISearchDisplayController」 所以這是我做過什麼: myUISearchDisplayController = [[MyUISearchDisplayController頁頭] initWithStyle:UITableViewStyleGrouped]; \t self.searchDisplayController.searchResultsTableView = myUISearchDisplayController.searchResultsTableView; 但它給我的錯誤,「/RootViewController.m:472:錯誤:對象無法設置 - 只讀屬性或沒有設置發現 」 有沒有解決方法? – Noam 2010-09-26 07:16:38
@David,我不認爲這是可能的要覆蓋Objective-C中的屬性,只能覆蓋**方法**。 – 2010-09-26 09:32:30
是啊,我看起來你運氣不好。即使你爲只讀屬性編寫setters,他們只是繼續調用自己。 – 2010-09-27 17:09:56
重寫-searchResultsTableView
將無法正常工作,因爲UISearchDisplayController
直接訪問它的表視圖實例變量,而不調用方法。
UISearchDisplayController
的指定初始值設定項似乎是一個私有方法,-initWithSearchBar:contentsController:searchResultsTableViewStyle:
,它設置_searchResultsTableViewStyle
實例變量。此實例變量用於創建搜索結果表視圖。公共初始化程序調用此方法,傳遞UITableViewStylePlain
。
直接調用私有指定的初始化或設置實例變量可能會得到從App Store拒絕的應用程序,所以你可能而是試圖重寫公共初始化,並呼籲
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"searchResultsTableViewStyle"];
這個工作對我來說:
創建延伸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
。
這算作使用私人API嗎? – chakrit 2012-03-27 11:25:30
這個工作對我(的iOS 5.0):
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
這是一個很好的見解,並且真正簡化了搜索的使用酒吧在我的應用程序。謝謝! – Luke 2011-05-29 00:14:16