将searchDisplayController表视图样式更改为分组?

Noa*_*oam 10 iphone uitableview uisearchbar uisearchdisplaycontroller

我有一个searchDisplayController搜索a UITableView.

输入搜索字词后,我可以看到UITableView包含搜索结果的其他字词.但是,我希望这UITableView是GROUPED,而不是PLAIN(默认情况下是这样).

我该怎么做呢?

小智 15

这对我有用(iOS 5.0):

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
        forKey:@"_searchResultsTableViewStyle"];
Run Code Online (Sandbox Code Playgroud)

  • `_searchResultsTableViewStyle`似乎没有记录......这会通过Apple的批准吗? (6认同)

chr*_*tfr 14

如果 - 像我一样 - 你认为普通的TableView太难看了,你也可以放弃使用SearchDisplayController.

我只是:

  • 插入一个空查看一个searchBar和一个TableView,就像我们通常为IBOutlet做的那样
  • 选择文件的所有者作为两者的委托.
  • 在开始时,节的数量是0([myTab count])然后我使用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
Run Code Online (Sandbox Code Playgroud)

毕竟,最简单的解决方案往往是最好的......