Change the searchDisplayController table view style to grouped?

10,534

Solution 1

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...

Solution 2

This worked for me (iOS 5.0):

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

Solution 3

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.

Solution 4

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

Share:
10,534
Noam
Author by

Noam

Updated on June 30, 2022

Comments

  • Noam
    Noam almost 2 years

    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?

  • Noam
    Noam over 13 years
    so basically there is absolutely no way to change it to a grouped style? that's absurd that apple would refrain us from doing such a thing..
  • Noam
    Noam over 13 years
    "Then just use YourUISearchDisplayController instead of UISearchDisplayController." So this is what I did: myUISearchDisplayController = [[MyUISearchDisplayController alloc] initWithStyle:UITableViewStyleGrouped]; self.searchDisplayController.searchResultsTableView = myUISearchDisplayController.searchResultsTableView; But it gives me the error that "/RootViewController.m:472: error: object cannot be set - either readonly property or no setter found " Is there a work-around?
  • Jacob Relkin
    Jacob Relkin over 13 years
    @David, I don't think it's possible to override properties in Objective-C, it's only possible to override methods.
  • Jason O. Jensen
    Jason O. Jensen over 13 years
    Yeah it looks like you're out of luck. Turns out even if you write setters for read-only properties they just keep calling themselves.
  • Jason O. Jensen
    Jason O. Jensen over 13 years
    As an afterthought: you can ask apple to change it, though I don't know what the odds are there.
  • Luke
    Luke almost 13 years
    This is a great insight and really streamlined the use of a search bar in my application. Thanks!
  • chakrit
    chakrit about 12 years
    Does that count as using a private api?
  • jcdmb
    jcdmb almost 12 years
    That's actually the best solution in this post. THANKS!
  • Nicu Surdu
    Nicu Surdu over 11 years
    _searchResultsTableViewStyle doesn't seem to be documented... Will this pass Apple's approval ?
  • gossainapps
    gossainapps almost 11 years
    I used this, and my app was approved. In my opinion this should be documented by Apple.
  • Evan Nagle
    Evan Nagle over 10 years
    I don't see any reason they should reject an app for doing this, but the danger is that the names change in future versions of iOS and you have to change your app to keep up.
  • Evan Nagle
    Evan Nagle over 10 years
    You could try adding a standalone method to your derived class instead of trying to override a readonly property (which you can't): -[YourUISearchDisplayController setSearchResultsTableViewStyle:]