I have a searchDisplayController
that searches a UITableView
.
开发者_运维问答After entering the search terms, I can see another UITableView
that contains the search results. However, I want this UITableView
to be GROUPED, not PLAIN (like it is by default).
How do I do this?
This worked for me (iOS 5.0):
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
If - like me - you think the plain TableView was way too ugly, you can also abandon the use of SearchDisplayController.
I just:
- inserted in an empty View a searchBar and a TableView as we usually do for IBOutlet
- selected the File's owner as delegate for both of them.
- At the beginin the number of section is 0 ([myTab count]) then I used reloadData and this time myTab is populated by the result.
[self.resultTableView reloadData]
;
Here you can find all the method I used from the delegates
@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
After all, the simplest solution is often the best...
This works for me:
Create a class which extends UISearchDisplayController:
@interface RVSearchDisplayController : UISearchDisplayController
@end
@implementation RVSearchDisplayController
-(UITableView *) searchResultsTableView {
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
return [super searchResultsTableView];
}
@end
Then add a UISearchDisplayController
to your table using IB, and change its Custom Class to RVSearchDisplayController
in Identity Inspector.
You could try to create a subclass of UISearchDisplayController and make searchResultsTableView searchable
in any .h file add:
@interface YourUISearchDisplayController : UISearchDisplayController {
UITableView * searchResultsTableView;
}
@property (nonatomic) UITableView * searchResultsTableView;
@end;
Then just use YourUISearchDisplayController instead od UISearchDisplayController.
Note: you might have to use (nonatomic, retain), (nonatomic, assign), or (nonatomic, copy). I'm not really sure
This is not possible as the searchResultsTableView
property is readonly
.
Overriding -searchResultsTableView
won't work, because UISearchDisplayController
accesses its table view instance variable directly, without calling the method.
The designated initializer for UISearchDisplayController
appears to be a private method, -initWithSearchBar:contentsController:searchResultsTableViewStyle:
, which sets the _searchResultsTableViewStyle
instance variable. This instance variable is used in creating the search results table view. The public initializer calls this method, passing UITableViewStylePlain
.
Directly calling the private designated initializer or setting the instance variable would likely get an application rejected from the App Store, so you might instead try overriding the public initializer and calling
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"searchResultsTableViewStyle"];
精彩评论