をsearchDisplayController
検索する がありUITableView
ます。
検索用語を入力すると、検索結果を含む別の用語が表示UITableView
されます。UITableView
ただし、これを PLAIN ではなく GROUPEDにしたい(デフォルトのように)。
どうすればいいですか?
をsearchDisplayController
検索する がありUITableView
ます。
検索用語を入力すると、検索結果を含む別の用語が表示UITableView
されます。UITableView
ただし、これを PLAIN ではなく GROUPEDにしたい(デフォルトのように)。
どうすればいいですか?
これは私にとってはうまくいきました(iOS 5.0):
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
私のように、単純な 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
結局のところ、最も単純なソリューションが最善の場合が多いのです...
これは私のために働く:
UISearchDisplayController を拡張するクラスを作成します。
@interface RVSearchDisplayController : UISearchDisplayController
@end
@implementation RVSearchDisplayController
-(UITableView *) searchResultsTableView {
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
return [super searchResultsTableView];
}
@end
UISearchDisplayController
次に、IB を使用してテーブルに a を追加しRVSearchDisplayController
、Identity Inspector でそのカスタム クラスを に変更します。
UISearchDisplayController のサブクラスを作成して、searchResultsTableView を検索可能にすることができます。
任意の .h ファイルに次を追加します。
@interface YourUISearchDisplayController : UISearchDisplayController {
UITableView * searchResultsTableView;
}
@property (nonatomic) UITableView * searchResultsTableView;
@end;
次に、UISearchDisplayController の代わりに YourUISearchDisplayController を使用します。
注: (非アトミック、保持)、(非アトミック、割り当て)、または (非アトミック、コピー) を使用する必要がある場合があります。よくわからない
メソッドを呼び出さずにテーブルビューインスタンス変数に直接アクセスする-searchResultsTableView
ため、オーバーライドは機能しません。UISearchDisplayController
の指定された初期化子は、インスタンス変数を設定UISearchDisplayController
するプライベートメソッドであるように見えます。このインスタンス変数は、検索結果テーブルビューの作成に使用されます。パブリック初期化子はこのメソッドを呼び出し、を渡します。-initWithSearchBar:contentsController:searchResultsTableViewStyle:
_searchResultsTableViewStyle
UITableViewStylePlain
プライベート指定イニシャライザーを直接呼び出すか、インスタンス変数を設定すると、アプリケーションがApp Storeから拒否される可能性が高いため、代わりにパブリックイニシャライザーをオーバーライドして呼び出してみてください。
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"searchResultsTableViewStyle"];
searchResultsTableView
プロパティが であるため、これは不可能ですreadonly
。